|
现有如下图所示一张表:
最终要实现下面这样的编码结果:
- --要求最终的结果,自动编码
- ID p_id NAME CODE
- 1 1 -1 A 03
- 2 2 1 B1 0301
- 3 5 2 C1 030101
- 4 6 2 C2 030102
- 5 3 1 B2 0302
- 6 7 3 D1 030201
- 7 4 1 B3 0303
复制代码
实现代码如下:
- select id,
- p_id,
- name,
- decode(p_id,
- -1,
- code,
- code ||
- replace(substr(sys_connect_by_path(pos, '.'),
- instr(sys_connect_by_path(pos, '.'), '.', 1, 2)),
- '.',
- '0')) new_code
- from (select id,
- p_id,
- name,
- last_value(code ignore nulls) over(order by rownum) code,
- row_number() over(partition by p_id order by rownum) pos
- from t_test)
- start with p_id = -1
- connect by prior id = p_id;
复制代码
|
|