【问题标题】:How can I create 1 ID for a group of customers in a column?如何为列中的一组客户创建 1 个 ID?
【发布时间】:2018-08-22 15:44:36
【问题描述】:

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

  • 有时我们可以根据合同拥有不同的客户。 示例: 凯伦和她的男朋友会有合同。
  • 有时一组客户可能有不同的合同。 示例:Karen 和 Will 与我签订了多份合同。

这是表格:

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

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

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

我什至尝试使用多选,不存在但什么都没有。

【问题讨论】:

  • 不清楚Karen 和Will 有什么不同idCustomer(在你的第一张桌子上)?您为相同的客户分配不同的 ID?

标签: sql sql-server uniqueidentifier


【解决方案1】:

看来我已经理解你的要求了。你仍然应该多扔一些样本数据,让所有人都清楚。在现有数据中添加更多不同的示例数据。

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

样本数据,

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

【讨论】:

  • 非常感谢@KumarHarsh 两者都工作得很好,但我使用了第一种方法。
  • @It'smeAndYou,是的,应该始终首选基于集合的方法。
【解决方案2】:

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

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

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2020-08-04
    • 2015-06-03
    • 2023-01-09
    • 1970-01-01
    • 1970-01-01
    • 2020-08-10
    • 2020-03-15
    相关资源
    最近更新 更多