很久以前就知道有cube这个函数,但一直没机会使用到它,今天做个树形的报表终于用上了。
报表按A B两列分组统计,A B列关系是多对多,要求A作父列,B做子列 显示成树形,然后再交换显示
效果如图
一开始考虑用 over (partition by ) 但发现需要进行子查询,后想到rollup 联想到cube
经过多次考虑完善,终于出炉
select decode(grouping(s.fname_l2),1,decode(grouping(u.fname_l2),1,'总计',u.fname_l2),s.fname_l2) 制单人,
decode(grouping(u.fname_l2),1,decode(grouping(s.fname_l2),1,'总计',s.fname_l2),u.fname_l2) 发货组织,
grouping(u.fname_l2) guname,grouping(s.fname_l2) gsname,
rtrim(u.fname_l2 ||'!'||s.fname_l2,'!') FlongnumberA,
rtrim(s.fname_l2 ||'!'||u.fname_l2,'!') FlongnumberB,
count(distinct b.FOrderCustomerID) 商家数量,count(distinct b.fnumber) 订单数量,
sum(decode(a.FBaseStatus,4,1,0)) 行审核数量,sum(decode(a.FBaseStatus,7,1,0)) 行关闭数量,
sum(a.FLocalTaxAmount) 总金额
from T_SD_SaleOrder b
inner join T_SD_SaleOrderEntry a on a.fparentid=b.fid
left join T_PM_User u on b.FCreatorID=u.fid
left join T_ORG_Storage s on a.FStorageOrgUnitID=s.fid
where b.fbizdate<=enddate and b.fbizdate>=startdate
and (b.FBaseStatus=4 or b.FBaseStatus=7)--审核 关闭
and b.fnumber not like '*%'
group by cube(u.fname_l2,s.fname_l2)
上面,decode 可以用case when 替换 在多个条件的情况下会少好多代码