postgresql7.1
select site_node__url(s.node_id)
from site_nodes s, apm_packages a
where s.object_id = a.package_id
and a.package_key = 'adserver'
update adv_log
set display_count = display_count + 1
where adv_key = :adv_key
and entry_date = trunc (current_timestamp)
insert into adv_log
(adv_key, entry_date, display_count)
values (:adv_key,
trunc (current_timestamp),
(select 1
where 0 = (select count (*)
from adv_log
where adv_key = :adv_key
and entry_date = trunc (current_timestamp))))
insert into adv_user_map (user_id, adv_key, event_time, event_type)
values (:user_id,:adv_key,current_timestamp,'d')
select map.adv_key, track_clickthru_p, target_url, display_count
from adv_group_map map, advs_todays_log log, advs
where group_key = :group_key
and map.adv_key = advs.adv_key
and map.adv_key = log.adv_key
UNION
select map.adv_key, track_clickthru_p, target_url, 0 as display_count
from adv_group_map map, advs
where group_key = :group_key
and map.adv_key = advs.adv_key
and (select count(*) from adv_log where advs.adv_key=adv_log.adv_key)=0
order by display_count asc
limit 1