我有一个返回24行的查询。我想将第一个12 SUM
的{{1}}划分为index
下一个12 SUM
。我这样使用index
,但我无法过滤ROW_NUMBER
。我怎么能这样做?
row
感谢
答案 0 :(得分:1)
由于您的查询返回24行尝试像这样
Select t.*,(Sum(Case when row<=12 then Index else 0 End)/
Sum(Case when row>12 then Index else 0 End)) as Result from
(
SELECT ROW_NUMBER() OVER(ORDER BY [Year] DESC,[Month] DESC) AS Row,[Year], [Month],
PCode_9, [Index]
FROM tbl08
WHERE PCode = @Code AND
(([Year] * 12 + [Month]) <= @CurrentYearMonth) AND
(([Year] * 12 + [Month]) >= @FCurrentYearMonth) AND row > 12 <-- ERROR
ORDER BY [Year] DESC,[Month] DESC
) t