[英]How to put max(number) condition into where clause in SQL Server 2008
My query is: 我的查询是:
Select
res.*, s.Description as GroupSpecialty,
MAX(CASE WHEN Period = @Q9 THEN cases END) OVER (PARTITION BY Group,Zone) as CompletedVolume
from
res
What I need to do is to eliminate all the data where CompletedVolume < 50. 我需要做的是消除CompletedVolume <50时的所有数据。
When I write where CompletedVolume >= 50
, it says there is no column as CompletedVolume
. 当我where CompletedVolume >= 50
写时,它说没有列作为CompletedVolume
。 When I write MAX(CASE WHEN Period = @Q9 THEN cases END) OVER (PARTITION BY Group, Zone) >= 50
, it says 当我写MAX(CASE WHEN Period = @Q9 THEN cases END) OVER (PARTITION BY Group, Zone) >= 50
,它说
Windowed functions can only appear in the SELECT or ORDER BY clauses 窗口函数只能出现在SELECT或ORDER BY子句中
So I couldn't provide this condition. 因此,我无法提供此条件。 How can I do that? 我怎样才能做到这一点? Thanks. 谢谢。
Use your query as a derived table 使用查询作为派生表
SELECT *
FROM
(select res.*, s.Description as GroupSpecialty,
MAX(CASE WHEN Period=@Q9 THEN cases END) OVER (PARTITION BY Group,Zone) as CompletedVolume
from res) DerivedTable
WHERE CompletedVolume < 50
Or CTE 或CTE
;WITH cte
AS
(
select res.*, s.Description as GroupSpecialty,
MAX(CASE WHEN Period=@Q9 THEN cases END) OVER (PARTITION BY Group,Zone) as CompletedVolume
from res
)
SELECT * from cte
where completedvolume < 50
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.