统计SQL的完善问题

wing7742 wing7742 墨衍会员 · AI 创作全网分发 墨衍智能分发 通过墨衍一键同步至各平台 可同步平台 CSDN微信公众号微博知乎掘金百家号博客园抖音小红书 你的文章也可以这样分发 墨衍会员 ¥399起/年,写一次发全网 我也要 + 加入社区

select c.[month],c.product_name,c.target_name,
c.product_item_name ZXNAME,c.zx ZXVALUE,
'' CJNAME,0 CJVALUE,
'' YJNAME,0 YJVALUE
from (
select [month],product_name,target_name,
max(zx) zx,
max(cj) cj,
max(total) total
from
(select
datepart(mm,user_time) [month],product_name,target_name,product_item_name,
count(*) zx,
count(case when state='SUCCESS' then state end) cj,
sum(sum_money) total
from vi_sale_view
where datepart(yyyy,user_time)=2010
and project_pkid<>0
group by datepart(mm,user_time),product_name,target_name,product_item_name
) a
group by [month],product_name,target_name
) b

left join

(select
datepart(mm,user_time) [month],product_name,target_name,product_item_name,
count(*) zx,
count(case when state='SUCCESS' then state end) cj,
sum(sum_money) total
from vi_sale_view
where datepart(yyyy,user_time)=2010
and project_pkid<>0
group by datepart(mm,user_time),product_name,target_name,product_item_name
) c on b.[month]=c.[month] and b.product_name=c.product_name
and b.target_name=c.target_name and b.zx=c.zx

union all

select c.[month],c.product_name,c.target_name,
'' ZXNAME,0 ZXVALUE,
c.product_item_name CJNAME,c.cj CJVALUE,
'' YJNAME,0 YJVALUE
from (
select [month],product_name,target_name,
max(zx) zx,
max(cj) cj,
max(total) total
from
(select
datepart(mm,user_time) [month],product_name,target_name,product_item_name,
count(*) zx,
count(case when state='SUCCESS' then state end) cj,
sum(sum_money) total
from vi_sale_view
where datepart(yyyy,user_time)=2010
and project_pkid<>0
group by datepart(mm,user_time),product_name,target_name,product_item_name
) a
group by [month],product_name,target_name
) b

left join

(select
datepart(mm,user_time) [month],product_name,target_name,product_item_name,
count(*) zx,
count(case when state='SUCCESS' then state end) cj,
sum(sum_money) total
from vi_sale_view
where datepart(yyyy,user_time)=2010
and project_pkid<>0
group by datepart(mm,user_time),product_name,target_name,product_item_name
) c on b.[month]=c.[month] and b.product_name=c.product_name
and b.target_name=c.target_name and b.cj=c.cj

union all

select c.[month],c.product_name,c.target_name,
'' ZXNAME,0 ZXVALUE,
'' CJNAME,0 CJVALUE,
c.product_item_name YJNAME,c.total YJVALUE
from (
select [month],product_name,target_name,
max(zx) zx,
max(cj) cj,
max(total) total
from
(select
datepart(mm,user_time) [month],product_name,target_name,product_item_name,
count(*) zx,
count(case when state='SUCCESS' then state end) cj,
sum(sum_money) total
from vi_sale_view
where datepart(yyyy,user_time)=2010
and project_pkid<>0
group by datepart(mm,user_time),product_name,target_name,product_item_name
) a
group by [month],product_name,target_name
) b

left join

(select
datepart(mm,user_time) [month],product_name,target_name,product_item_name,
count(*) zx,
count(case when state='SUCCESS' then state end) cj,
sum(sum_money) total
from vi_sale_view
where datepart(yyyy,user_time)=2010
and project_pkid<>0
group by datepart(mm,user_time),product_name,target_name,product_item_name
) c on b.[month]=c.[month] and b.product_name=c.product_name
and b.target_name=c.target_name and b.total=c.total
order by [month],product_name,target_name


如上SQL得到结果
month product_name target_name ZXNAME ZXVALUE CJNAME CJVALUE YJNAME YJVALUE
----- ------------ ---------- --------- ---- ------ ------- ------ --------
1 AA a1 null 0 aa 2 null 0
1 AA a1 bb 3 null 0 null 0
1 AA a1 null 0 null 0 cc 44
1 BB b2 dd 3 null 0 null 0
1 BB b2 de 3 null 0 null 0
1 BB b2 null 0 null 0 ee 5
1 BB b2 null 0 ff 3 null 0
......
最终希望统计结果为
1 AA a1 bb 3 aa 2 cc 44
1 BB b2 dd,de 3 ff 3 ee 5

请问应该怎么对查询的数据统计
...全文

相关推荐

评论 21
收藏 已收藏
分享
返回顶部
wing7742
疑难问题 22,298 成员 121,727 内容