2012-03-16 34 views
0

我有臺這樣的增加行數(比如說25 000)

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

我需要的表像下面的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 

經過一番行數(說25000)組列行計數具有提高

(EX-如果行計數跨過25000的組列的值必須改變到下一個數,即25001 - 2,50001 - 3)

如何在SQL Server中編寫查詢?

回答

3

您可以使用row_number來生成數字並進行一些計算。
這將使5行的組。

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