I'm trying to divide records of data into groups.
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.
The final result should be like this
I tried something like this
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
