日期:2014-05-18  浏览次数:20523 次

查询统计问题
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