假设我的所有字段名称都是正确的。
DesCount
GroupType
我的问题:当我使用新创建的列GroupType
并在rank = rownumber over (partition
函数中引用它时,它会给我这个错误说:
Msg 207, Level 16, State 1, Line 6 Invalid column name 'GroupType'.
Msg 207, Level 16, State 1, Line 6 Invalid column name 'GroupType'.
以下是我的SQL查询:
select * from (
select
D1,[DesCount], Type, [Caused], [Comm], DATE, 'IC' As GroupType,
rank = row_number() over (partition by Date, [GroupType] order by date, [GroupType], [DesCount] desc)
from Z_DefectCount
) t
where rank <= 3
谢谢大家的帮助
答案 0 :(得分:0)
您不能在定义的同一级别使用列别名。但是你不需要它,因为它是不变的。您也不需要在partition by
表达式中重复order by
个表达式。
这应该做你想要的:
select *
from (select D1, [DesCount], Type, [Caused], [Comm], DATE, 'IC' As GroupType,
rank = row_number() over (partition by Date order by [DesCount] desc)
from Z_DefectCount
) t
where rank <= 3