重复数据时如何提供序列号

时间:2018-11-24 12:32:41

标签: sql

如果我的表具有此值,我需要生成seqno列

ClientId clinetLocation seqno
001        Abc           1
001        BBc           2
001        ccd           3
002        Abc           1
002        BBc           2
003        ccd           1

2 个答案:

答案 0 :(得分:1)

您正在寻找row_number()函数:

select ClientId, clinetLocation,
       row_number() over (partition by ClientId order by clinetLocation) as seqnum
from t;

这是大多数数据库中可用的标准功能。

答案 1 :(得分:0)

一个选项是对与这些列有关的分组行进行计数:

select count(1) over ( order by ClientId, ClientLocation ) as seqno, 
       ClientId, ClientLocation
  from tab
 group by ClientId, ClientLocation;

其中ClientIdClientLocation的组合似乎是唯一的。

Rextester Demo