我正在尝试编写一个SQLSELECT查询,该查询具有一个批号列,每5行递增一次,我尝试在动态行号上使用hacky整数除法,但始终无法解决第一个批号增量的问题4行而不是5行(参见图片)。
RowNumber 1-5应该为1,6-10应该为2,依此类推。
有什么想法可以实现这一目标吗?
select * ROW_NUMBER() OVER (Order by (select 0)) AS RowNumber, 1 + convert(int, (ROW_NUMBER() OVER (Order by (select 0)) / 5)) as BatchNumber, * from WorkQueue
这应该工作:
select ROW_NUMBER() OVER (Order by (select 0)) AS RowNumber, (case when convert(int, (ROW_NUMBER() OVER (Order by (select 0)) % 5))=0 then 0 else 1 end) + convert(int, (ROW_NUMBER() OVER (Order by (select 0)) / 5)) as BatchNumber, * from WorkQueue