统计SQL的完善问题
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
请问应该怎么对查询的数据统计
...全文
相关推荐
基于NL2SQL的兵棋数据智能统计分析方法研究.pdf
基于NL2SQL的兵棋数据智能统计分析方法研究.pdf
JSP+SQL网站流量统计管理系统(源代码+论文).rar
随着因特网的迅猛发展,网络变得越来越普遍,但结构却越来越复杂,而且现在涌现出大量的各种各样网络的应用、硬件、平台和协议,各个企业或部门不得不投入一个或多个网络管理者去维护和控制其网络。如何能将网站从具体和繁琐的网络管理中解脱出来.同时提高网络的经济效益就显得很迫切和重要,这就要求在网络建设的同时加强网管建设。
php流量统计功能的实现代码
用php实现的流量统计功能代码,本代码仅供学习交流,其中必有不妥之处。请见谅
SQL语句在病案首页数据查询中的相关应用.pdf
SQL语句在病案首页数据查询中的相关应用.pdf
大数据时代统计工作存在的问题及对策(1).docx
大数据时代统计工作存在的问题及对策(1).docx


疑难问题
