select
n.notification_uid::text as notification_uid
n.notification
n.descr
n.url
n.timeout_sec
n.contragent_id
k.external_uid
to_char(n.dt_show_from, 'YYYY-MM-DD"T"HH24:MI:SS.FF3TZHTZM') as dt_show_from
to_char(n.dt_show_to, 'YYYY-MM-DD"T"HH24:MI:SS.FF3TZHTZM') as dt_show_to
from notification n
left outer join contragent k on (n.contragent_id=k.contragent_id)
where 1=1
AND (n.dt_show_from <= CURRENT_TIMESTAMP OR n.dt_show_from IS NULL)
AND (n.dt_show_to >= CURRENT_TIMESTAMP OR n.dt_show_to IS NULL)
AND (k.external_uid = OR n.contragent_id IS NULL)
order by dt_show_from desc
limit #maxrows#
select count(*) as cnt
from notification n
left outer join contragent k on (n.contragent_id=k.contragent_id)
where 1=1
AND (n.dt_show_from <= CURRENT_TIMESTAMP OR n.dt_show_from IS NULL)
AND (n.dt_show_to >= CURRENT_TIMESTAMP OR n.dt_show_to IS NULL)
AND (k.external_uid = OR n.contragent_id IS NULL)