我是SQL的初学者,我在这里坚持实习项目。
我有一份与我的服务公司有不同合同的客户清单。
这是桌子:
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
我什至尝试使用多选,但不存在但什么也没有。我只是不知道该怎么办。我需要帮助
谢谢!
答案 0 :(得分:0)
似乎需要创建一个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组成。这是一项正在进行的工作... ...
答案 1 :(得分:0)
看来我已经理解了您的要求。不过,您仍然应该多扔一些样本数据,以使所有人都绝对清楚。在现有的样本数据中再添加几个其他样本数据。
您应该使用其他示例数据进行测试,并让我知道它是否无效。
样本数据
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')
;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
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