There are two relatively similar requests. The second query displays the summary data of the first. Is it possible to somehow optimize them and execute everything in one query? And display the amount at the end of each column?
Main query:
select round(hz,2) HZ,bz,round(h0,1)HO,naim, round(sum(pf),2) PF,round(sum(pt),2) PT, round(sum(pdm),2) PDM,round(sum(pdp),2) PDP,round(sum(pbm),2)PBM,round(sum(pbp),2) PBP, round(sum(tp)/60,2) TP,round(sum(pf)/sum(tp)*60,1)SFP from (select r.hz,r.bz,r.h0,m.naim,r.pf,r.pt,r.pdm,r.pdp,r.pbm ,r.pbp, case when ((r.tz-p.ts)*1440 < 10) and ((r.i_rez3/60) < 10) then (case when rn > 1 then ((r.ts-p.ts)*1440) else ((r.ts-r.tz)*1440) end) when ((r.tz-p.ts)*1440 > 10) and ((r.i_rez3/60) < 10) then (case when rn > 1 then (((r.ts-r.tz)*1440)+10) else((r.ts-r.tz)*1440)end) when ((r.tz-p.ts)*1440 < 10) and ((r.i_rez3/60) > 10) then (case when rn > 1 then ((((r.ts-p.ts)*1440)-(r.i_rez3/60-10)))else((((r.ts-r.tz)*1440)-(r.i_rez3/60-10))) end) when ((r.tz-p.ts)*1440 > 10) and ((r.i_rez3/60) > 10) then (case when rn > 1 then ((((r.ts-r.tz))*1440+10-(r.i_rez3/60-10))) else((((r.ts-r.tz))*1440-(r.i_rez3/60-10)))end) end as tp from zxp.rulon r,zxp.mc m,zxp.rulon p where r.ts between to_date('14.04.16 22:00:00','dd.mm.yy hh24:mi:ss') and to_date('15.04.16 7:00:00','dd.mm.yy hh24:mi:ss')+1 and r.ms = m.ukey and p.ts = ( select max(pp.ts) from zxp.rulon pp where pp.ts <= r.tz and pp.ts > r.tz-1)) group by hz,h0,bz,naim order by hz,h0,bz,naim Duplicate with amount:
select 'Итого:',round(sum(pf),2)"Р физ.",round(sum(pt),2)"Р теор.", round(sum(pdm),2)"Р доп. -",round(sum(pdp),2)"Р доп. +",round(sum(pbm),2)"Р бр. -",round(sum(pbp),2)"Р бр. +", round(sum(tp)/60,2)"Длит.",round(sum(pf)/sum(tp)*60,1)"Произв." from (select r.pf,r.pt,r.pdm,r.pdp,r.pbm,r.pbp, case when ((r.tz-p.ts)*1440 < 10) and ((r.i_rez3/60) < 10) then (case when rn > 1 then ((r.ts-p.ts)*1440) else ((r.ts-r.tz)*1440) end) when ((r.tz-p.ts)*1440 > 10) and ((r.i_rez3/60) < 10) then (case when rn > 1 then (((r.ts-r.tz)*1440)+10) else((r.ts-r.tz)*1440)end) when ((r.tz-p.ts)*1440 < 10) and ((r.i_rez3/60) > 10) then (case when rn > 1 then ((((r.ts-p.ts)*1440)-(r.i_rez3/60-10)))else((((r.ts-r.tz)*1440)-(r.i_rez3/60-10))) end) when ((r.tz-p.ts)*1440 > 10) and ((r.i_rez3/60) > 10) then (case when rn > 1 then ((((r.ts-r.tz))*1440+10-(r.i_rez3/60-10))) else((((r.ts-r.tz))*1440-(r.i_rez3/60-10)))end) end as tp from zxp.rulon r,zxp.rulon p where r.ts between to_date('14.04.16 22:00:00','dd.mm.yy hh24:mi:ss') and to_date ('15.04.16 7:00:00','dd.mm.yy hh24:mi:ss') and p.ts =(select max(pp.ts) from zxp.rulon pp where pp.ts <= r.tz and pp.ts > r.tz-1))