如何在sql server的select列中写入条件?

时间:2014-03-25 15:20:14

标签: sql sql-server-2008 tsql

我有一个像

这样的Select语句
select AVG(CAST(DATEDIFF(Day,CreatedDate,FirstTouchDate) AS BIGINT) else null end) AS avgdaystofirstattempt,
       AVG(CAST(DATEDIFF(Day,CreatedDate,Completeddate) AS BIGINT)) AS AvgDaystoComplete
from Table1

如何为列avgdaystofirstattempt编写条件 - FirstTouchdate不喜欢'%1900%'在选择本身但不在where子句中。

提前致谢

1 个答案:

答案 0 :(得分:0)

您可以使用CASE选择性地在汇总语句中包含数据。你的问题并不是很清楚,但我认为你想要这样的事情:

SELECT AVG(
             CASE 
               WHEN DATEDIFF(Day,CreatedDate,FirstTouchDate) < 1900 THEN NULL
               ELSE CAST(DATEDIFF(Day,CreatedDate,FirstTouchDate) AS BIGINT)
               END
           ) AS avgdaystofirstattempt,
       AVG(CAST(DATEDIFF(Day,CreatedDate,Completeddate) AS BIGINT)) AS AvgDaystoComplete
FROM Table1