我目前的结果:
没有。 | A 栏 | B 栏 | C 栏 |
---|---|---|---|
1 | A | AB | col_amount |
2 | col_amount | ||
3 | col_amount | ||
4 | B | AS | col_amount |
5 | col_amount | ||
6 | T | 电视 | col_amount |
我想得到:
没有。 | A 栏 | B 栏 | C 栏 |
---|---|---|---|
1 | A | AB | col_amount |
col_amount | |||
col_amount | |||
2 | B | AS | col_amount |
col_amount | |||
3 | T | 电视 | col_amount |
根据上一个问题,您可以在
DENSE_RANK
表达式中使用 CASE
分析函数。
给定样本数据:
CREATE TABLE table_name (Column_A, Column_B, Column_C) AS
SELECT '01', 'AB', 'col_amount' FROM DUAL UNION ALL
SELECT '01', 'AB', 'col_amount' FROM DUAL UNION ALL
SELECT '01', 'AC', 'col_amount' FROM DUAL UNION ALL
SELECT '02', 'OA', 'col_amount' FROM DUAL UNION ALL
SELECT '03', 'OE', 'col_amount' FROM DUAL UNION ALL
SELECT '03', 'OE', 'col_amount' FROM DUAL UNION ALL
SELECT '04', 'OE', 'col_amount' FROM DUAL UNION ALL
SELECT '04', 'XX', 'col_amount' FROM DUAL;
然后,如果您想根据
column_a
和 column_b
的变化进行编号,则:
SELECT CASE b_rn WHEN 1 THEN DENSE_RANK() OVER (ORDER BY column_a, column_b) END
AS no,
CASE a_rn WHEN 1 THEN column_a END AS column_a,
CASE b_rn WHEN 1 THEN column_b END AS column_b,
column_c
FROM (
SELECT column_a,
column_b,
column_c,
ROW_NUMBER() OVER (PARTITION BY column_a ORDER BY column_b, column_c)
AS a_rn,
ROW_NUMBER() OVER (PARTITION BY column_a, column_b ORDER BY column_c)
AS b_rn
FROM table_name
)
哪个输出:
不 | COLUMN_A | COLUMN_B | COLUMN_C |
---|---|---|---|
1 | 01 | AB | col_amount |
空 | 空 | 空 | col_amount |
2 | 空 | 交流 | col_amount |
3 | 02 | 办公自动化 | col_amount |
4 | 03 | OE | col_amount |
空 | 空 | 空 | col_amount |
5 | 04 | OE | col_amount |
6 | 空 | XX | col_amount |
或者,如果您只想根据
column_a
的变化进行编号,那么您可以使用:
SELECT CASE a_rn WHEN 1 THEN DENSE_RANK() OVER (ORDER BY column_a) END
AS no,
CASE a_rn WHEN 1 THEN column_a END AS column_a,
CASE b_rn WHEN 1 THEN column_b END AS column_b,
column_c
FROM (
SELECT column_a,
column_b,
column_c,
ROW_NUMBER() OVER (PARTITION BY column_a ORDER BY column_b, column_c)
AS a_rn,
ROW_NUMBER() OVER (PARTITION BY column_a, column_b ORDER BY column_c)
AS b_rn
FROM table_name
)
哪个输出:
不 | COLUMN_A | COLUMN_B | COLUMN_C |
---|---|---|---|
1 | 01 | AB | col_amount |
空 | 空 | 空 | col_amount |
空 | 空 | 交流 | col_amount |
2 | 02 | 办公自动化 | col_amount |
3 | 03 | OE | col_amount |
空 | 空 | 空 | col_amount |
4 | 04 | OE | col_amount |
空 | 空 | XX | col_amount |