提供表格:
# Id Val
1 1111111111 Andre
2 1111111111 Bart
3 1111111111 Corry
4 2222222222 Donald
5 2222222222 Eric
6 3333333333 Fiona
我想选择一个带有指示符的表:将具有相同ID的行分组,即得到类似的内容:
# Id Val
1 1111111111 Andre
1 1111111111 Bart
1 1111111111 Corry
2 2222222222 Donald
2 2222222222 Eric
3 3333333333 Fiona
在此我提出了解决方案:
SELECT
(
row_number() OVER (ORDER BY id) - row_number() OVER (PARTITION BY id ORDER BY id) + 1
) nGroupedToRow,
row_number() OVER (ORDER BY id) nRow,
row_number() OVER (PARTITION BY id ORDER BY id) nNumDupl,
id,
val
FROM table1
产生:
nGroupedToRow nRow nNumDupl Id Val
1 1 1 1111111111 Andre
1 2 2 1111111111 Bart
1 3 3 1111111111 Corry
4 4 1 2222222222 Donald
4 5 2 2222222222 Eric
6 6 1 3333333333 Fiona
认为这可能对某人有所帮助...也许有人可以对此进行改进...也许有人提出了类似的问题,但是尽管堆栈溢出的其他解决方案使我想到了这一点,但我找不到它...
答案 0 :(得分:0)
SELECT
(
row_number() OVER (ORDER BY id) - row_number()
OVER (PARTITION BY id ORDER BY id) + 1
) nGroupedToRow,
row_number() OVER (ORDER BY id) nRow,
row_number() OVER (PARTITION BY id ORDER BY id) nNumDupl,
id,
val
FROM table1