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

帮忙看下这个查询解决思路

2012-05-04 
帮忙看下这个查询SQL codeSELECT COUNT(case when b.Docu_Status2 then b.Docu_Status else 0 end) AS

帮忙看下这个查询

SQL code
SELECT COUNT(case when b.Docu_Status='2' then b.Docu_Status else 0 end) AS Expr1,COUNT(case when b.Docu_Status='5' then b.Docu_Status else 0 end) AS Expr2,COUNT(case when b.Docu_Status='11' then b.Docu_Status else 0 end) AS Expr3,COUNT(case when b.Docu_Status='14' then b.Docu_Status else 0 end) AS Expr4FROM UDP_Project as a INNER JOINUDP_Document as b ON a.Project_Id = b.Docu_ForProwhere a.Project_Djsj between '2011-12-27 0:00:00' and '2012-4-16 0:00:00'

为什么我统计到的Expr1-Expr4结果全部相同,case when b.Docu_Status='2' then b.Docu_Status else 0 end这样的条件没用吗?

[解决办法]
SQL code
SELECT SUM(case when b.Docu_Status='2' then b.Docu_Status else 0 end) AS Expr1,SUM(case when b.Docu_Status='5' then b.Docu_Status else 0 end) AS Expr2,SUM(case when b.Docu_Status='11' then b.Docu_Status else 0 end) AS Expr3,SUM(case when b.Docu_Status='14' then b.Docu_Status else 0 end) AS Expr4FROM UDP_Project as a INNER JOINUDP_Document as b ON a.Project_Id = b.Docu_ForProwhere a.Project_Djsj between '2011-12-27 0:00:00' and '2012-4-16 0:00:00'
[解决办法]
SQL code
SELECT sum(case when b.Docu_Status='2' then 1 else 0 end) AS Expr1,sum(case when b.Docu_Status='5' then 1 else 0 end) AS Expr2,sum(case when b.Docu_Status='11' then 1 else 0 end) AS Expr3,sum(case when b.Docu_Status='14' then 1 else 0 end) AS Expr4FROM UDP_Project as a INNER JOINUDP_Document as b ON a.Project_Id = b.Docu_ForProwhere a.Project_Djsj between '2011-12-27 0:00:00' and '2012-4-16 0:00:00' 

热点排行