2014-10-30 66 views
0

大家好,datepartcase when没有工作。我想这...
这工作:为什么datepart提取小时数不起作用?

select status_name ,count(pk_lead_id) as leads where lm.lead_created_date between @sDate and @eDate and datepart(hh,lead_created_date) in (9,10,11,12,13,14,15,16,17,18,19) and datepart(hh,lead_created_date) in (20,21,22,23,0,1,2,3,4,5,6,7,8) from lead_master group by status_name

但是,这并没有给正确的输出:

select status_name ,count(pk_lead_id) as leads --'9am to 8pm' and '8pm to 9am' ,count(case when cast(right(left(lead_created_date,13),2) as int) in (9,10,11,12,13,14,15,16,17,18,19)then 1 else 0 end)as morning ,count(case when cast(right(left(lead_created_date,13),2) as int) in (20,21,22,23,0,1,2,3,4,5,6,7,8)then 1 else 0 end)as night from lead_master group by status_name

我需要查询哪里datepart条件在case when ...

回答

1

为什么你在日期时间栏上应用RIGHTLEFT

case when cast(right(left(lead_created_date,13),2) as int) in (9,10,11,12,13,14,15,16,17,18,19)then 1 else 0 end 

更改您的查询:

select status_name 
     ,count(pk_lead_id) as leads --'9am to 8pm' and '8pm to 9am' 
     ,sum(case when datepart(hh,lead_created_date) in (9,10,11,12,13,14,15,16,17,18,19) then 1 else 0 end)as morning 
     ,sum(case when datepart(hh,lead_created_date) in (20,21,22,23,0,1,2,3,4,5,6,7,8) then 1 else 0 end)as night 
from lead_master group by status_name 
+0

朋友,甚至还我都试过了。案件不是那样的。您可以将自己的代码粘贴到SQL中并尝试一下。 – Vikrant 2014-10-31 05:02:07

+0

你的回答没有帮助 – Vikrant 2014-10-31 10:54:19

+1

我的猜测是你想排序一个COUNTIF函数。如果是这样,请使用SUM,而不是COUNT。 – 2014-10-31 11:52:11

相关问题