首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 数据库 > SQL Server >

查询统计有关问题

2012-05-05 
查询统计问题SQL codeSELECT sum(case when b.Docu_Status2 then 1 else 0 end) AS 扫描,sum(case when

查询统计问题

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 

热点排行