简体   繁体   中英

How to write the condition in select column's in sql server?

I have a Select statement like

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

how can i write the condition for column avgdaystofirstattempt - FirstTouchdate not like '%1900%' in the select itself but not in where clause.

thanks in advance

You can selectively include data in an aggregate statement by using CASE . It's not really clear from your question, but I think you want something like this:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM