我想在表格中为每个唯一的值组合添加唯一编号。
示例数据:
create table tmp (
id int primary key,
a varchar,
b varchar,
c varchar,
d varchar,
f int
);
insert into tmp values (1,'a','b','e','h',1);
insert into tmp values (2,'a','b','e','h',2);
insert into tmp values (3,'a','b','e','h',3);
insert into tmp values (4,'b','c','f','i',2);
insert into tmp values (5,'b','c','f','i',1);
insert into tmp values (6,'b','c','f','i',2);
insert into tmp values (7,'c','d','g','j',3);
insert into tmp values (8,'c','d','g','j',1);
insert into tmp values (9,'c','d','g','j',2);
现在我需要为列a, b, c, d
的每个唯一组合分配编号,并返回列id
和gid
(组标识符)
示例输出(例如,ID为1,2 3的行具有相同的列a, b, c, d
组合,因此这些行应具有相同的组标识符):
id;gid
1;2
2;2
3;2
4;3
5;3
6;3
7;1
8;1
9;1
我已经找到了以下解决方案,但我认为应该有更好(更快)的方式:
select
id,
gid
from
tmp
join (
select
a, b, c, d, row_number() over() as gid
from
tmp
group by
a, b, c, d) gids using (a, b, c, d)
答案 0 :(得分:4)
您可以使用dense_rank()功能:
select
id, dense_rank() over(order by a,b,c,d) as gid
from tmp
<强> sql fiddle demo 强>