当达到一定数量时,如何重新编号行?

时间:2014-05-13 20:32:29

标签: sql oracle row-number

我想为组中包含的所有项创建两个分层的行号。例如,我想对一个州的所有城市进行编号,但是将它们组成3组。最终结果看起来像这样:

STATE | NUM1 | NUM2 | CITY
-------------------------------------
NY    | 1    | 1    | Albany
NY    | 1    | 2    | Buffalo
NY    | 1    | 3    | Syracuse
NY    | 2    | 1    | Rochester
NY    | 2    | 2    | Ithaca

......等等。我想将NUM2限制为3,但让NUM1尽可能高。我现在正在使用的是什么:

SELECT
    STATE,
    CITY,
    ROW_NUMBER() OVER(PARTITION BY STATE ORDER BY CITY) AS CITY_NUM
FROM
    T1
;

这没关系,但对于拥有大量“城市”的“州”,我得到了一些非常高的数字,并希望所有内容都水平放在三列之内。有什么想法吗?

1 个答案:

答案 0 :(得分:2)

只需使用整数运算:

SELECT STATE, CITY,
       1 + FLOOR((ROW_NUMBER() OVER(PARTITION BY STATE ORDER BY CITY) - 1)/ 3) AS CITY_NUM_1,
       1 + MOD(ROW_NUMBER() OVER(PARTITION BY STATE ORDER BY CITY) - 1, 3) AS CITY_NUM_2
FROM T1;

对于所需的方案,使用测试数据,查询将按如下方式工作:

WITH TAB AS 
(
  SELECT 'NY' STATE, 'Albany' CITY FROM DUAL UNION
  SELECT 'NY' STATE, 'Buffalo' CITY FROM DUAL UNION
  SELECT 'NY' STATE, 'Syracuse' CITY FROM DUAL UNION
  SELECT 'NY' STATE, 'Rochester' CITY FROM DUAL UNION
  SELECT 'NY' STATE, 'Ithaca' CITY FROM DUAL UNION
  SELECT 'IN' STATE, 'Mumbai' CITY FROM DUAL UNION
  SELECT 'IN' STATE, 'Chennai' CITY FROM DUAL UNION
  SELECT 'IN' STATE, 'Delhi' CITY FROM DUAL UNION
  SELECT 'IN' STATE, 'Kolkatta' CITY FROM DUAL 
)
SELECT STATE, CITY,
   1 + FLOOR((ROW_NUMBER() OVER(PARTITION BY STATE ORDER BY CITY) - 1)/ 3) AS CITY_NUM_1,
   1 + MOD(ROW_NUMBER() OVER(PARTITION BY STATE ORDER BY CITY) - 1, 3) AS CITY_NUM_2
FROM TAB;