declare
l_meas_for_dt_am0 NUMBER;
l_meas_for_dt_am1 NUMBER;
l_meas_for_dt_am2 NUMBER;
v_dt DATE := date'2015-01-01';
begin
with w_t as (
select date'2015-01-01' as dt, 100 as meas from dual union all
select date'2015-01-01' as dt, 200 as meas from dual union all
select date'2015-02-01' as dt, 200 as meas from dual union all
select date'2015-02-01' as dt, 300 as meas from dual union all
select date'2015-03-01' as dt, 400 as meas from dual union all
select date'2015-03-01' as dt, 500 as meas from dual
)
select
sum(case when dt = add_months(v_dt, 0) then meas else 0 end), -- фиктивное выравнивание для повышения аккуратности :)
sum(case when dt = add_months(v_dt, 1) then meas else 0 end),
sum(case when dt = add_months(v_dt, 2) then meas else 0 end)
/*...*/
into l_meas_for_dt_am0, l_meas_for_dt_am1, l_meas_for_dt_am2
from
w_t;
dbms_output.put_line(l_meas_for_dt_am0 || ', ' || l_meas_for_dt_am1 || ', ' || l_meas_for_dt_am2);
end;
|