我有一张这样的桌子我希望根据cksid和guid的组合对它进行排名。
cksid guid
----------- -----------
1 301
1 301
1 301
2 303
2 303
3 303
4 303
4 303
4 303
输出应该像
cksid guid rank
----------- ----------- ----------
1 301 1
1 301 2
1 301 3
2 303 1
2 303 2
3 303 1
4 303 1
4 303 2
4 303 3
答案 0 :(得分:5)
试试这个:
您只需使用row_number函数
select cksid ,guid ,
row_number() over (partition by cksid ,guid order by (select 0)) as rank
from <Table>
row_number()需要order by子句。在这里,您没有第三列可供订购。所以我只是放(选择0),它将以任何顺序对输出进行排序,这对我们来说无关紧要。我们本可以通过cksid,guid给出订单,但是我觉得哪个有点过头,因为我们可以通过给予(选择0)得到相同的结果
答案 1 :(得分:1)
SELECT cksid, guid,
row_number() OVER (PARTITION BY cksid,guid ORDER BY cksid,guid)
FROM youtable;
答案 2 :(得分:0)
create table test11(cksid int,guid int)
INSERT INTO test11
VALUES(1,301),
(1,301),
(1,301),
(2,303),
(2,303),
(3,303),
(4,303),
(4,303),
(4,303)
select *,ROW_NUMBER() over (partition by cksid,guid order by guid) as ranks from test11