SQL Server-无法基于第一个新列创建其他新列

SQL Server-无法基于第一个新列创建其他新列,sql,sql-server,Sql,Sql Server,我正在选择两列,并根据列说明中的类别创建一个附加列RPM。然后,我想基于RPM创建一个名为MaxRPM的新列。如果删除第3行,该命令工作正常,但不会创建MaxRPM 我收到以下错误:状态1,第1行,无效列名“RPM” SQL Server命令: select [ID], [Date], max(case when Description = 'RPM' then Value end) as RPM, max(RPM) over (partition by [ID] order b

我正在选择两列,并根据列
说明
中的类别创建一个附加列
RPM
。然后,我想基于
RPM
创建一个名为
MaxRPM
的新列。如果删除第3行,该命令工作正常,但不会创建
MaxRPM

我收到以下错误:
状态1,第1行,无效列名“RPM”

SQL Server命令:

select [ID], [Date],
    max(case when Description = 'RPM' then Value end) as RPM,
    max(RPM) over (partition by [ID] order by [Date] ROWS BETWEEN 5 PRECEDING AND current row) as MaxRPM
from [DB].[Table]
where [Date] > '2019-01-01'
group by [ID], [Date]

为什么我在第二行创建新列时会出现此错误?任何帮助都将不胜感激。

您可以尝试以下方法-因为您不能使用别名创建其他列

with cte as
(
select [ID], [Date],
    max(case when Description = 'RPM' then Value end) as RPM
    from [DB].[Table]
where [Date] > '2019-01-01'
group by [ID], [Date]
)

select *,max(RPM) over (partition by [ID] order by [Date] ROWS BETWEEN 5 PRECEDING AND current row) as MaxRPM
from cte

我认为Fami命令中的第3行需要删除,否则会出现同样的错误

with cte as
(
select [ID], [Date],
    max(case when Description = 'RPM' then Value end) as RPM
from [DB].[Table]
where [Date] > '2019-01-01'
group by [ID], [Date]
)

select *,max(RPM) over (partition by [ID] order by [Date] ROWS BETWEEN 5 PRECEDING AND current row) as MaxRPM
from cte