问题描述
column0 | column1 | column2 | column3 |
---|---|---|---|
一 | x | 1 | 1 |
一 | y | 1 | 2 |
一 | z | 1 | 3 |
b | x | 2 | 1 |
b | y | 2 | 2 |
c | x | 3 | 1 |
c | y | 3 | 2 |
c | z | 3 | 3 |
column2 -> column0 的行数
column3 -> 分区 column0 & column1 的行数
我试过了,但没有用
SELECT ROW_NUMBER() OVER (PARTITION BY column0 ORDER BY column0 ) column2,ROW_NUMBER() OVER (PARTITION BY column0,column1 ORDER BY column0,column1) column3
FROM DUAL
你有什么想法吗?
解决方法
对 DENSE_RANK
使用 column2
,对 ROW_NUMBER
使用 column3
:
SELECT
column0,column1,DENSE_RANK() OVER (ORDER BY column0) AS column3,ROW_NUMBER() OVER (PARTITION BY column0 ORDER BY column1) AS column4
FROM yourTable;
,
使用 row_number:
SQL> with test (col0,col1) as
2 (select 'a','x' from dual union all
3 select 'a','y' from dual union all
4 select 'a','z' from dual union all
5 select 'b','x' from dual union all
6 select 'b','y' from dual
7 )
8 select col0,col1,9 row_number() over (partition by col1 order by col0) col2,10 row_number() over (partition by col0 order by col1) col3
11 from test
12 order by col0,col1
13 /
COL0 COL1 COL2 COL3
----- ------ ---------- ----------
a x 1 1
a y 1 2
a z 1 3
b x 2 1
b y 2 2
SQL>