我不知道我要命名这个请求,因此我没有找到任何答案。 基本上是一个常见的陈述,如:
SELECT A,B,C,D,E FROM TABLE
示例结果:
A B C D E
1 1 2 3 4
1 2 3 4 5
1 2 7 8 9
2 1 4 5 6
我如何分类'通过某些列(在示例A和B中),因此省略列值?
首选结果:
A B C D E
1 1 2 3 4
1 2 3 4 5
7 8 9
2 1 4 5 6
答案 0 :(得分:1)
您可以使用LAG
analytic function访问以前的行值。见下面的例子:
SELECT case
when LAG(a, 1, NULL)
OVER(ORDER BY a, b, c, d, e) = a and LAG(b, 1, NULL)
OVER(ORDER BY a, b, c, d, e) = b then
null
else
a
end new_a,
case
when LAG(a, 1, NULL)
OVER(ORDER BY a, b, c, d, e) = a and LAG(b, 1, NULL)
OVER(ORDER BY a, b, c, d, e) = b then
null
else
b
end new_b,
c,
d,
e
FROM t_table t
ORDER BY a, b, c, d, e;
答案 1 :(得分:1)
在你的情况下,我想让A,B排序的结果是有意义的吗? 在这种情况下,您可以使用:
SELECT DECODE(RN,1,A,NULL) AS A,
DECODE(RN,1,B,NULL) AS B,
C,
D,
E
FROM
(SELECT A,
B,
row_number() over (partition BY A,B order by A,B) AS RN,
C,
D,
E
FROM
(SELECT * FROM TEST_TABLE ORDER BY A,B
)
);