SQl查询将结果分解为特定大小的组

时间:2013-05-02 19:21:56

标签: sql sql-server

我需要编写一个查询来识别5个不同大小的组。我知道我可以使用NTILE将我的设置分成相同大小的组但我需要这样的东西

Total Records say 1,000,000

Group 1 100,000 Rows
Group 2 200,000 Rows
Group 3 300,000 Rows
Group 4 400,000 Rows

由于

1 个答案:

答案 0 :(得分:2)

好吧,使用row_number()和逻辑:

select (case when seqnum <= 100000 then 'Group1'
             when seqnum <= 100000 + 200000 then 'Group2'
             when seqnum <= 100000 + 200000 + 300000 then 'Group3'
             when seqnum <= 100000 + 200000  + 300000 + 400000 then 'Group4'
         end) as GroupName,
       t.*
from (select t.*,
             row_number() over (order by <your criteria>) as seqnum
      from t
     ) t