title: Divide Ids into multiple groups based on a number in SQL Server
body: <p>I'm trying to divide records of data into groups.</p>
  <p>Each group size has 4 records, and the last group maybe less than 4,
  with numbering rows in each group, and count Ids in each group.</p>
  <p>The final result should be like this</p>
  <p><a href="https://i.stack.imgur.com/m8TyW.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/m8TyW.png" alt="Final Result" /></a></p>
  <p>I tried something like this</p>
  <pre><code>declare @t int 
  set @t = (select count(*) from mytable) 
  
  declare @s int 
  set @s = 4
  
  select 
      t.Id
      ,@s over (order by t.Id asc) as GroupId 
      ,case when (row_number() over (order by t.Id asc ) %@s ) = 0 then 4 else (row_number() over (order by t.Id asc) %@s ) end  as RowNum
      ,(count Ids in each group) IdCount
  from
      mytable t
  group by 
      t.Id
  order by 
      t.Id asc
  </code></pre>