Referring to a Column Alias in a WHERE Clause

后端 未结 9 2128
你的背包
你的背包 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:01

    For me, the simplest way to use ALIAS in WHERE class is to create a subquery and select from it instead.

    Example:

    WITH Q1 AS (
        SELECT LENGTH(name) AS name_length,
        id,
        name
        FROM any_table
    )
    
    SELECT id, name, name_length form Q1 where name_length > 0
    

    Cheers, Kel

提交回复
热议问题