简体   繁体   English

列的唯一组合的编号

[英]Numbering of unique combinations of columns

I would like to add unique number to every unique combination of values in table. 我想为表中的每个唯一值组合添加唯一编号。

Sample data: 样本数据:

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);

Now I need to assign number to every unique combination of columns a, b, c, d and return columns id and gid (group identificator) 现在我需要为列a, b, c, d和返回列idgid (组标识符)的每个唯一组合分配编号

Sample output (for example rows with id 1,2 3 have the same combination of columns a, b, c, d and as a result these rows should have the same group identificator): 样本输出(例如,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

I've figured out the following solution, but I think there should be a better (and faster) way: 我已经找到了以下解决方案,但我认为应该有更好(更快)的方式:

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)

SQLFiddle SQLFiddle

You can use dense_rank() function: 你可以使用dense_rank()函数:

select
     id, dense_rank() over(order by a,b,c,d) as gid
from tmp

sql fiddle demo sql小提琴演示

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM