0

24 行を返すクエリがあります。SUM最初の 12indexSUM次の 12に分割したいindex。私はROW_NUMBERこのように使用しますが、フィルタリングできませんrow。どうすればこれを行うことができますか?

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

ありがとう

4

1 に答える 1

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
于 2013-06-03T04:51:36.640 に答える