select B.enterprise_code,
B.enterprise_name,
sum(B.h0_overnum) AS over00,
sum(B.h1_overnum) AS over01,
sum(B.h2_overnum) AS over02,
sum(B.h3_overnum) AS over03,
sum(B.h4_overnum) AS over04,
sum(B.h5_overnum) AS over05,
sum(B.h6_overnum) AS over06,
sum(B.h7_overnum) AS over07,
sum(B.h8_overnum) AS over08,
sum(B.h9_overnum) AS over09,
sum(B.h10_overnum) AS over10,
sum(B.h11_overnum) AS over11,
sum(B.h12_overnum) AS over12
from table B
where B.monitor_time = to_char(sysdate - 1, 'yyyy-mm-dd')
group by B.enterprise_code,B.enterprise_name
合并用sum,统计个数用count。
之后列转行
select enterprise_code, enterprise_name, OVERNUM, MONITOR_TIME
from (select B.enterprise_code,
B.enterprise_name,
sum(B.h0_overnum) AS over00,
sum(B.h1_overnum) AS over01,
sum(B.h2_overnum) AS over02,
sum(B.h3_overnum) AS over03,
sum(B.h4_overnum) AS over04,
sum(B.h5_overnum) AS over05,
sum(B.h6_overnum) AS over06,
sum(B.h7_overnum) AS over07,
sum(B.h8_overnum) AS over08,
sum(B.h9_overnum) AS over09,
sum(B.h10_overnum) AS over10,
sum(B.h11_overnum) AS over11,
sum(B.h12_overnum) AS over12
from table B
where B.monitor_time = to_char(sysdate - 1, 'yyyy-mm-dd')
group by B.enterprise_code,B.enterprise_name) A unpivot(OVERNUM for MONITOR_TIME in(over00,
over01,
over02,
over03,
over04,
over05,
over06,
over07,
over08,
over09,
over10,
over11,
over12))