读书人

查询统计有关问题

发布时间: 2012-05-05 17:21:10 作者: rapoo

查询统计问题

SQL code
SELECT sum(case when b.Docu_Status='2' then 1 else 0 end) AS 扫描,sum(case when b.Docu_Status='5' then 1 else 0 end) AS 质检,sum(case when b.Docu_Status='11' then 1 else 0 end) AS 总质检,sum(case when b.Docu_Status='14' then 1 else 0 end) AS 完成 FROM UDP_Project as a INNER JOIN UDP_Document as b ON a.Project_Id = b.Docu_ForPro

如上SQL语句,我希望查询出来的结果再增加一列‘总计’,就是把这几个sum的和加起来
类似于这样
SQL code
SELECT sum(case when b.Docu_Status='2' then 1 else 0 end) AS 扫描,sum(case when b.Docu_Status='5' then 1 else 0 end) AS 质检,sum(case when b.Docu_Status='11' then 1 else 0 end) AS 总质检,sum(case when b.Docu_Status='14' then 1 else 0 end) AS 完成,sum(...) as 总计FROM UDP_Project as a INNER JOIN UDP_Document as b ON a.Project_Id = b.Docu_ForPro
有没有办法实现~

[解决办法]
SQL code
SELECT sum(case when b.Docu_Status='2' then 1 else 0 end) AS 扫描,sum(case when b.Docu_Status='5' then 1 else 0 end) AS 质检,sum(case when b.Docu_Status='11' then 1 else 0 end) AS 总质检,sum(case when b.Docu_Status='14' then 1 else 0 end) AS 完成,count(1) as 总计FROM UDP_Project as a INNER JOIN UDP_Document as b ON a.Project_Id = b.Docu_ForPro
[解决办法]
SELECT sum(case when b.Docu_Status='2' then 1 else 0 end) AS 扫描,
sum(case when b.Docu_Status='5' then 1 else 0 end) AS 质检,
sum(case when b.Docu_Status='11' then 1 else 0 end) AS 总质检,
sum(case when b.Docu_Status='14' then 1 else 0 end) AS 完成,
count(1) as 总计
FROM UDP_Project as a INNER JOIN UDP_Document as b ON a.Project_Id = b.Docu_ForPro

+++
[解决办法]
SQL code
SELECT sum(case when b.Docu_Status='2' then 1 else 0 end) AS 扫描,sum(case when b.Docu_Status='5' then 1 else 0 end) AS 质检,sum(case when b.Docu_Status='11' then 1 else 0 end) AS 总质检,sum(case when b.Docu_Status='14' then 1 else 0 end) AS 完成,SUM(case b.Docu_Status when '2' then 1                         when '5' then 1                         when '11' then 1                         when '14' then 1                         ELSE 0 END) as 总计FROM UDP_Project as a INNER JOIN UDP_Document as b ON a.Project_Id = b.Docu_ForPro 

读书人网 >SQL Server

热点推荐