Increase row count in sql server after some count (say 25,000)

橙三吉。 提交于 2019-12-11 18:09:28

问题


I have table like this

col1 col2 col3
3   5    8    
4   5    5    
5   5    5    
3   3    3    
4   5    6 

I need to get table like below in SQL Server

col1 col2 col3  group 
  3   5    8    1
  4   5    5    1
  5   5    5    2
  3   3    3    2
  4   5    6    3

After some row count (say 25000 ) group column row count has to increase

(ex- if row count crosses 25,000 the group column value has to change to next number ie 25,001 - 2, 50001 - 3)

How to write a query in SQL Server?


回答1:


You can use row_number to generate numbers and the do some calculations.
This will make on group of 5 rows.

select Column1, 
       Column2,
       1 + ((row_number() over(order by Column3) - 1) / 5)
from YourTable


来源:https://stackoverflow.com/questions/9733642/increase-row-count-in-sql-server-after-some-count-say-25-000

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!