SQl Query to break results into specifically sized groups

I need to write a query to identify 5 groups of different size. I know I can use NTILE to break my set up into equal size groups but I need something like this

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

Thanks

Answers


Well, use row_number() and logic on top of that:

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

Need Your Help

How can I set up 'bake' with MAMP on OS X with projects in 'Sites'?

cakephp mamp cakephp-bake .profile

I'm trying to set up 'cake bake' on OS X. I'm using MAMP and every time I try to 'bake', I get this:

Ruby on Rails - after_validation if valid?

ruby-on-rails ruby validation

Right now, from what I know, after_validation will be called even if the model fails the validations. Is there a way to only call it if the model is valid? I tried adding return false unless self.v...

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.