有人可以帮助使用SQL函数。我需要一个可以在每个循环中循环并执行具有不同日期的查询的函数。我想检查字段,如果不是null然后返回字段或行,如果为null我想继续循环不同。
这是我想要的一个例子:
create or replace function getCounterValue(id integer, datee text) RETURNS varchar(20) AS $$
DECLARE
cvalue varchar(20);
BEGIN
for x IN 1..365 LOOP
select cr.counter_value into cvalue
from snmp_printer p
left join snmp_counters_scan cs on(p.id=cs.printer)
left join snmp_printer_model pm on(p.model=pm.id)
left join snmp_counter c on (pm.id=c.model)
left join snmp_counters_result cr
on (cr.snmp_counter=c.id and cr.counters_scan=cs.id)
where p.id = id
and c.name='PAGE COUNT'
and cs.id in (select max(pp.id) from snmp_counters_scan pp
where pp.scan_date <= cast(datee as date) -interval x ||'day'
group by printer);
if cvalue is not null then
return cvalue;
else
--continue
end if;
END LOOP;
RETURN cvalue;
END;
$$ LANGUAGE plpgsql;