除以两个间隔的SUM

时间:2013-06-03 04:47:34

标签: sql sql-server sql-server-2008

我有一个返回24行的查询。我想将第一个12 SUM的{​​{1}}划分为index下一个12 SUM。我这样使用index,但我无法过滤ROW_NUMBER。我怎么能这样做?

row

感谢

1 个答案:

答案 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