繁体   English   中英

列到位时出现无效列名错误

[英]Invalid column name error when column in place

我正在尝试 select 行,其中结束日期在一个范围内。 在我的 dbo.subscription 表中,我有 startdate 和 enddate 列。 当我使用 startdate 时,查询运行,但是当我使用 enddate 时,它说列名无效。

我尝试在 enddate 周围使用 '' 或 "" [],我也尝试将列的全名写出 (dbo.subscription.enddate),但似乎没有什么区别。

;WITH cte 
     AS (SELECT Membershipnumber as Id
               Row_number() 
                  OVER ( 
                    partition BY membershipnumber 
                    ORDER BY subscription.enddate DESC) AS rownumber 
         FROM   [dbo].[userprofile] 
                INNER JOIN dbo.subscription 
                        ON userprofile.id = subscription.userprofileid 
                INNER JOIN dbo.subscriptiontype 
                        ON subscriptiontype.id = subscription.subscriptiontypeid

        ) 
SELECT * 
FROM   cte 
WHERE  rownumber = 1 and enddate between'2014-12-31' and '2018-12-31'
order by Id

错误信息:

Msg 207, Level 16, State 1, Line 21
Invalid column name 'enddate'.
Msg 207, Level 16, State 1, Line 21
Invalid column name 'enddate'.

您最终的 SELECT 是SELECT FROM cte

cte只定义了两列: IdRowNumber

enddate未在cte中定义。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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