如何使用先行连接来颠倒顺序

时间:2019-04-02 19:50:33

标签: sql oracle connect-by

我有如下数据:

 ID          DATA          S_LEVEL 
1304819     M02-004           1
1304819     M01-003           2
1304819     M01-005           3
1304819     MT-M01-005        4
1304819     M01-004           5
1304819     M01-002           6
1304819     M01-009           7
1304819     M01-020           8
1304819     MT-3100-007A      9

我想使用按优先级和级别进行连接的输出如下:

 ID          DATA        S_LEVEL     D_LEVEL
1304819     M02-004         1           9
1304819     M01-003         2           8
1304819     M01-005         3           7
1304819     MT-M01-005      4           6
1304819     M01-004         5           5
1304819     M01-002         6           4
1304819     M01-009         7           3
1304819     M01-020         8           2
1304819     MT-3100-007A    9           1

1 个答案:

答案 0 :(得分:0)

使用row_number()进行D_level生成并应用订单

SELECT A.*, ROW_NUMBER () OVER (PARTITION BY PARTID ORDER BY MAX (S_LEVEL) DESC) AS D_LEVEL
  FROM (SELECT PARTID, DATA, S_LEVEL FROM TABLE 
       ) 
  GROUP BY PARTID,DATA,S_LEVEL ORDER BY PARTID,S_LEVEL,D_LEVEL DESC