如何为列中的一组客户创建 1 个 ID?

How can I create 1 ID for a group of customers in a column?

我有一份与我的服务公司签订不同合同的客户名单。

这里是 table:

idCustomer  idContract          NameCust
-----------------------------------------
1           A                   Karen
1           B                   Will
2           A                   Karen
2           B                   Will
3           C                   Steph
4           C                   Peter

但是因为 Karen 和 Will 可以有多个合同,所以我希望他们和其他客户组有一个唯一的 ID。结果table我要:

idCustomer  idContract  NameCust    Customer_GroupID
-----------------------------------------------------
1           A           Karen       1
1           B           Will        1
2           A           Karen       1
2           B           Will        1
3           C           Steph       2
4           C           Peter       2

我被卡住了,因为我尝试了不同的方法,但没有得到我需要的结果。我在论坛上发现有人使用 Dense_Rank 函数,但结果如下:

SELECT
    RANK() OVER (ORDER BY idCustomers) AS Customer_GroupID,
    IdCustomers,
    IdContract
FROM 
    Table

结果如下:

Cust_GroupID  idCustomer    idContract
--------------------------------------
1              1             A
2              1             B
1              2             A
2              2             B
3              3             C
3              4             C

我什至尝试使用多个 select,不存在但什么也没有。

似乎需要创建一个 NameCustGroup 的临时 table,例如

NameCustList  idCustomer idContract
Karen,Will    1          A,B
Karen,Will    2          A,B
Peter,Steph   3          C
Peter,Steph   4          C

然后用它来创建

Customer_GroupID  idCustomer  idContract
1                 1           A,B
1                 2           A,B
2                 3           C
2                 4           D

临时table似乎最难,因为NameCustList 需要由Common idCustomer 或Common idContract 组成。这是一项正在进行的工作... ...

看来我明白了你的要求。你仍然应该抛出更多的样本数据,让所有人都清楚。在现有数据中添加更多不同的样本数据。

您应该使用不同的示例数据进行测试,如果它不起作用请告诉我。

示例数据,

create table #test(idCustomer int,idContract varchar(50) , NameCust varchar(50))
insert into #test  (idCustomer ,idContract , NameCust ) VALUES
 (1,'A','Karen')
,(1,'B','Will' )
,(2,'A','Karen')
,(2,'B','Will' )
,(3,'C','Steph')
,(4,'C','Peter')

方法 1 - 基于集合的方法,

;with CTE as
(
select * 
,ROW_NUMBER()over(order by idCustomer)rn
from #test
)
,CTE1 as
(
select t.id,t.idContract,t.NameCust
, isnull(t1.idCustomer,t.idCustomer)customerGroupID 
from CTE t
outer apply(
select top 1 idCustomer 
from CTE t1 
where t1.id< t.id 
and((t.idCustomer=t1.idCustomer) 
or (t.idContract=t1.idContract)) 
order by t1.id 
)t1
)
,CTE2 AS(
select * 
,DENSE_RANK()OVER( order by customerGroupID )Customer_GroupID
from CTE1
)

select * from CTE2

方法 2- RBAR(使用游标),

create table #test1(id int identity(1,1),idCustomer int
,idContract varchar(50) , NameCust varchar(50),customer_Groupid int)

insert into #test1  (idCustomer ,idContract 
, NameCust,customer_Groupid ) 
select idCustomer ,idContract , NameCust,null 
from #test

DECLARE @idCustomer INT
DECLARE @idContract varchar(50)
DECLARE @id INT
declare @customer_Groupid int
DECLARE @getCustomer CURSOR
SET @getCustomer = CURSOR FOR
SELECT id, idCustomer,idContract
FROM #test1
OPEN @getCustomer
FETCH NEXT
FROM @getCustomer INTO @id, @idCustomer,@idContract
WHILE @@FETCH_STATUS = 0
BEGIN

select top 1 @customer_Groupid=customer_Groupid 
from #test1 where id<@id order by id desc

if  not exists(select 1 from #test1 where id<@id 
and (idCustomer=@idCustomer or idContract=@idContract))
BEGIN

select top 1 @customer_Groupid=customer_Groupid 
from #test1 where id<@id order by id desc
if(@customer_Groupid is not null)
set @customer_Groupid=@customer_Groupid+1

end


if(@customer_Groupid is null)
set @customer_Groupid=1

update #test1 set customer_Groupid=@customer_Groupid where id=@id

FETCH NEXT
FROM @getCustomer INTO @id, @idCustomer,@idContract
END
CLOSE @getCustomer
DEALLOCATE @getCustomer


select * from #test1

drop table #test1
drop table #test