选择记录的父级

时间:2018-09-28 09:25:00

标签: sql oracle

我有这个要求,我需要在select语句中附加一个新列。它保存父级的disp_order。

我目前有这个sql语句

SELECT DISTINCT order_no, 
                code, 
                disp_order, 
                lvl, 
                description 
           FROM tbl_pattern

+----------+------+------------+-----+-------------+
| order_no | code | disp_order | lvl | description |
+----------+------+------------+-----+-------------+
| RM001-01 | 1    | 0          | 1   | HK140904-1A |
| RM001-01 | 1    | 1          | 2   | HK140904-1B |
| RM001-01 | 1    | 2          | 3   | HK140904-1B |
| RM001-01 | 1    | 3          | 4   | HK140904-1C |
| RM001-01 | 1    | 4          | 5   | HK140904-1D |
| RM001-01 | 1    | 5          | 2   | HK140904-1E |
| RM001-01 | 1    | 6          | 3   | HK140904-1E |
| RM001-01 | 1    | 7          | 3   | HK140904-1X |
| RM001-01 | 1    | 8          | 4   | HK140904-1E |
| RM001-01 | 1    | 9          | 5   | HK140904-1E |
+----------+------+------------+-----+-------------+

父列在表上不存在,但我想使该级别的disp_order高于当前记录。

在示例表中,结果应如下所示:

+----------+------+------------+-----+-------------+--------+
| order_no | code | disp_order | lvl | description | parent |
+----------+------+------------+-----+-------------+--------+
| RM001-01 | 1    | 0          | 1   | HK140904-1A |        |
| RM001-01 | 1    | 1          | 2   | HK140904-1B |    0   |
| RM001-01 | 1    | 2          | 3   | HK140904-1B |    1   |
| RM001-01 | 1    | 3          | 4   | HK140904-1C |    2   |
| RM001-01 | 1    | 4          | 5   | HK140904-1D |    3   |
| RM001-01 | 1    | 5          | 2   | HK140904-1E |    0   |
| RM001-01 | 1    | 6          | 3   | HK140904-1E |    5   |
| RM001-01 | 1    | 7          | 3   | HK140904-1X |    5   |
| RM001-01 | 1    | 8          | 4   | HK140904-1E |    7   |
| RM001-01 | 1    | 9          | 5   | HK140904-1E |    8   |
+----------+------+------------+-----+-------------+--------+

通过提供的示例层次结构进行数据表示:

1
  └2
    └3
      └4
        └5
  └2
    └3
    └3
      └4
        └5

2 个答案:

答案 0 :(得分:2)

您可以使用相关子查询找到上一级的最新disp_order

SQL Fiddle

Oracle 11g R2架构设置

CREATE TABLE tbl_pattern ( order_no, code, disp_order, lvl, description ) AS
SELECT 'RM001-01', 1, 0, 1, 'HK140904-1A' FROM DUAL UNION ALL
SELECT 'RM001-01', 1, 1, 2, 'HK140904-1B' FROM DUAL UNION ALL
SELECT 'RM001-01', 1, 2, 3, 'HK140904-1B' FROM DUAL UNION ALL
SELECT 'RM001-01', 1, 3, 4, 'HK140904-1C' FROM DUAL UNION ALL
SELECT 'RM001-01', 1, 4, 5, 'HK140904-1D' FROM DUAL UNION ALL
SELECT 'RM001-01', 1, 5, 2, 'HK140904-1E' FROM DUAL UNION ALL
SELECT 'RM001-01', 1, 6, 3, 'HK140904-1E' FROM DUAL UNION ALL
SELECT 'RM001-01', 1, 7, 3, 'HK140904-1X' FROM DUAL UNION ALL
SELECT 'RM001-01', 1, 8, 4, 'HK140904-1E' FROM DUAL UNION ALL
SELECT 'RM001-01', 1, 9, 5, 'HK140904-1E' FROM DUAL;

查询1

SELECT t.*,
       ( SELECT MAX( disp_order )
         FROM   tbl_pattern p
         WHERE  p.order_no   = t.order_no
         AND    p.code       = t.code
         AND    p.lvl        = t.lvl - 1
         AND    p.disp_order < t.disp_order ) AS parent
FROM   tbl_pattern t

Results

| ORDER_NO | CODE | DISP_ORDER | LVL | DESCRIPTION | PARENT |
|----------|------|------------|-----|-------------|--------|
| RM001-01 |    1 |          0 |   1 | HK140904-1A | (null) |
| RM001-01 |    1 |          1 |   2 | HK140904-1B |      0 |
| RM001-01 |    1 |          2 |   3 | HK140904-1B |      1 |
| RM001-01 |    1 |          3 |   4 | HK140904-1C |      2 |
| RM001-01 |    1 |          4 |   5 | HK140904-1D |      3 |
| RM001-01 |    1 |          5 |   2 | HK140904-1E |      0 |
| RM001-01 |    1 |          6 |   3 | HK140904-1E |      5 |
| RM001-01 |    1 |          7 |   3 | HK140904-1X |      5 |
| RM001-01 |    1 |          8 |   4 | HK140904-1E |      7 |
| RM001-01 |    1 |          9 |   5 | HK140904-1E |      8 |

答案 1 :(得分:1)

自行加入您的数据。正确的加入条件是最重要的部分。然后,如果存在多个匹配项,则仅使用row_number()筛选出disp_order最大的行。

SQLFiddle demo

select disp_order, lvl, description, parent
  from (
    select t1.*, t2.disp_order parent, 
           row_number() over (partition by t1.disp_order 
                              order by t2.disp_order desc) rn
      from tbl_pattern t1 
      left join tbl_pattern t2 on t2.lvl = t1.lvl - 1 and t2.disp_order < t1.disp_order)
  where rn = 1
  order by disp_order