引用WHERE子句中的列别名SELECT logcount, logUserID, maxlogtm , DATEDIFF(day, maxlogtm, GETDATE()) AS daysdiffFROM statslogsummaryWHERE daysdiff > 120我得到“无效列名daysdiff”。Maxlogtm是一个datetime字段。是这些小事让我发疯。
3 回答
冉冉说
TA贡献1877条经验 获得超1个赞
SELECT logcount, logUserID, maxlogtm, DATEDIFF(day, maxlogtm, GETDATE()) AS daysdiffFROM statslogsummaryWHERE ( DATEDIFF(day, maxlogtm, GETDATE() > 120)
WHERE
SELECT
WHERE
SELECT
WHERE
SELECT *FROM( SELECT logcount, logUserID, maxlogtm, DATEDIFF(day, maxlogtm, GETDATE()) AS daysdiff FROM statslogsummary ) as innerTableWHERE daysdiff > 120
MMMHUHU
TA贡献1834条经验 获得超8个赞
如果要在WHERE子句中,您需要将其包装在子SELECT中,或CTE:
WITH LogDateDiff AS
(
SELECT logcount, logUserID, maxlogtm
, DATEDIFF(day, maxlogtm, GETDATE()) AS daysdiff
FROM statslogsummary
)
SELECT logCount, logUserId, maxlogtm, daysdiff
FROM LogDateDiff
WHERE daysdiff > 120
绝地无双
TA贡献1946条经验 获得超4个赞
outer apply
:
select s.logcount, s.logUserID, s.maxlogtm, a.daysdifffrom statslogsummary as s outer apply (select datediff(day, s.maxlogtm, getdate()) as daysdiff) as awhere a.daysdiff > 120
添加回答
举报
0/150
提交
取消