--------備忘錄
在常用的彙總報表中,會出現通過某些條件實現分組後,對數量金額進行小計以及總計
可以使用group by rollup(col1,col2,....coln) 來實現分組統計
若要對總計,小計等漢字的顯示進行控制,可以使用case()函數
select
tp.tp_xmbh as xmid,
tp.tp_ccbh as showid,
(case when tp.tp_xmbh is null then null else (select wp.name from ZYPWT2.PM_PROJECT_INFO wp where STATUS=1 and PI_CATEGORY='2101'and id=tp.tp_xmbh) end) as col1,
(case when tp.tp_xmbh is null then null else (select to_char(wp.pi_show_start_time,'yyyy-mm-dd')|| '-' ||to_char(wp.pi_show_end_time,'yyyy-mm-dd') from ZYPWT2.PM_PROJECT_INFO wp where STATUS=1 and PI_CATEGORY='2101'and id=tp.tp_xmbh) end) as col2,
(case when tp.tp_ccbh is null then null else (select ws.name from zypwt2.pm_show_info ws where ws.status=1 and ws.id= tp.tp_ccbh) end) as col3,
(case when tp.tp_ccbh is null then null else (select to_char(ws.si_show_start_time,'yyyy-mm-dd hh24:mi') from zypwt2.pm_show_info ws where ws.status=1 and ws.id= tp.tp_ccbh) end ) as col4,
(case when tp.tp_ccbh is null then null else (select ven.name from zypwt2.vm_venues_info ven where ven.status='1' and ven.id=(select ws.si_venue_id from zypwt2.pm_show_info ws where ws.status=1 and ws.id=tp.tp_ccbh)) end) as col5,
(case
when tp.tp_pjid is null and tp.tp_ccbh is null and tp.tp_xmbh is not null then '項目總計'
when tp.tp_pjid is null and tp.tp_ccbh is not null then '場次小計'
when tp.tp_pjid is null and tp.tp_ccbh is null and tp.tp_xmbh is null then '總計'
else to_char((select pri.pli_price from zypwt2.pm_price_level_info pri where pri.id=tp.tp_pjid and status='1')) end
) as col6,
sum(tp.tp_sqzs) as col7,
'¥'||sum(tp.tp_sqje) as col8,
sum(tp.tp_tpzs) as col9,
'¥'||sum(tp.tp_tpje) as col10
from crm_dd_tpxx tp
where tp.tp_khbh='decc16ad7f6747b3bf24e5ee379c6b94' and zt='1' group by rollup(tp.tp_xmbh,tp.tp_ccbh,tp.tp_pjid) order by tp.tp_xmbh,tp.tp_ccbh