在[{3}}
上扩展问题 DEPARTMENT | SUB_DEPT | EMPLOYEE | SALARY
DEPT1 | SUB_DEPT1 | ABC | 20000
DEPT1 | SUB_DEPT1 | DEF | 30000
DEPT1 | SUB_DEPT2 | GHI | 25000
DEPT1 | SUB_DEPT2 | JKL | 42000
DEPT2 | SUB_DEPT3 | MNO | 42500
DEPT2 | SUB_DEPT3 | PQR | 35000
DEPT3 | SUB_DEPT4 | STU | 36000
DEPT3 | SUB_DEPT5 | VWX | 36040
检索
DEPARTMENT | SUB_DEPT | EMPLOYEE | SALARY
DEPT1 | SUB_DEPT1 | ABC | 20000
| | DEF | 30000
| SUB_DEPT2 | GHI | 25000
| | JKL | 42000
DEPT2 | SUB_DEPT3 | MNO | 42500
| | PQR | 35000
DEPT3 | SUB_DEPT4 | STU | 36000
| SUB_DEPT5 | VWX | 36040
这意味着 1)包装必须适用于两列。 2)包装文本必须是第一行,即DEPT1必须仅出现在第一行,DEPT2出现在第5行。
请帮帮我。
提前致谢。
答案 0 :(得分:2)
您可以按照与原始帖子相同的流程进行操作。为每个唯一的部门创建一个row_number()
,然后为每个唯一的部门和 sub_dept分区执行相同的操作:
select
case when deptrn = 1 then department else '' end department,
case when SubDeptRn = 1 then sub_dept else '' end sub_dept,
employee,
salary
from
(
select department, sub_dept, employee, salary,
row_number() over(partition by department order by department, employee) DeptRn,
row_number() over(partition by department, sub_dept order by department, sub_dept, employee) SubDeptRn
from yourtable
) src
结果:
| DEPARTMENT | SUB_DEPT | EMPLOYEE | SALARY |
----------------------------------------------
| DEPT1 | SUB_DEPT1 | ABC | 20000 |
| | | DEF | 30000 |
| | SUB_DEPT2 | GHI | 25000 |
| | | JKL | 42000 |
| DEPT2 | SUB_DEPT3 | MNO | 42500 |
| | | PQR | 35000 |
| DEPT3 | SUB_DEPT4 | STU | 36000 |
| | SUB_DEPT5 | VWX | 36040 |