CTE列名无效

时间:2011-06-30 02:32:18

标签: sql sql-server-2008 common-table-expression

我完全陷入了这个问题。我查看了其他问题,但找不到一个回答这个问题(无论如何我都能理解)。我的查询中有以下CTE,但MaxUserID在3个位置被红色曲折,它使用了“无效列名'MaxUserID'”错误。它应该表示的列是一个int,如果有帮助的话。有什么建议吗?

我正在使用SQL Server 2008。

;with TotalCount(TotalCount,MaxUserID)

as

(
    Select ISNULL(count(distinct uCPR.HeaderID), 0) as TotalCount, MaxUserID
            from ClientFeedback.dbo.UnitCountCPR uCPR
            where 
                uCPR.DHDate between @StartDate and @EndDateMod
                and uCPR.TargetID in (@StatusID)
                and uCPR.UserID = MaxUserID
                and uCPR.DTStamp between @StartDate and @EndDateMod
                and uCPR.ClientID in (@ClientID)
      group by MaxUserID

)

1 个答案:

答案 0 :(得分:1)

刷新智能感知缓存,然后使用表别名选择coluumns。此外,count永远不会为null,你的IsNull对你没有任何好处。你有没有理由而不是等于?