Referring to a Column Alias in a WHERE Clause

后端 未结 9 2042
你的背包
你的背包 2020-11-22 05:38
SELECT logcount, logUserID, maxlogtm
   , DATEDIFF(day, maxlogtm, GETDATE()) AS daysdiff
FROM statslogsummary
WHERE daysdiff > 120

I get

9条回答
  •  失恋的感觉
    2020-11-22 06:18

    The most effective way to do it without repeating your code is use of HAVING instead of WHERE

    SELECT logcount, logUserID, maxlogtm
       , DATEDIFF(day, maxlogtm, GETDATE()) AS daysdiff
    FROM statslogsummary
    HAVING daysdiff > 120
    

提交回复
热议问题