0

我在表中有以下内容TopTenBrands

Brands             Cust_ID
------------------------------    
Hitachi           16402, 16407    
Hitachi           16409, 16428    
JVC               19301, 19308    
LG                21001, 21007    
LG                21001, 21007, 21008    
LG                21001, 21008    
Panasonic         27909, 27912    
Philips           28501, 28518    
Philips           28513, 28516

在上表中,我有一个 LG 品牌,它拥有三行

LG                21001, 21007    
LG                21001, 21007, 21008    
LG                21001, 21008

但是在输出表中,我希望结果是休闲

Brands             Cust_ID
------------------------------
Hitachi           16402, 16407    
Hitachi           16409, 16428    
JVC               19301, 19308    
LG                21001, 21007, 21008    
Panasonic         27909, 27912    
Philips           28501, 28518    
Philips           28513, 28516

我想删除重复项并保留唯一 ID,并且我不希望 ID 按品牌分组。

我该怎么做?

4

3 回答 3

1

尝试这个

Declare @t  table(Brands Varchar(20),Cust_Id Varchar(100))
Insert Into @t 
Select 'Hitachi','16402, 16407' Union ALL Select 'Hitachi','16409, 16428' Union All
Select 'JVC','19301, 19308' Union All Select 'LG','21001, 21007' Union All    
Select 'LG','21001, 21007, 21008'  Union All Select 'LG','21001, 21008' Union All
Select 'Panasonic','27909, 27912 ' Union All Select 'Philips','28501, 28518'  Union All  
Select 'Philips','28513, 28516'


;WITH CTE AS
(
SELECT 
Rn = ROW_NUMBER() OVER(PARTITION BY LTRIM(RTRIM(Y.SplitCust_Id)),Brands ORDER BY (SELECT 1))
 ,X.Brands
 ,X.Cust_Id
 ,SplitCust_Id =LTRIM(RTRIM(Y.SplitCust_Id))
 FROM(SELECT *,CAST('<X>'+REPLACE(F.Cust_Id,',','</X><X>')+'</X>' AS XML) AS xmlfilter
      FROM @t F)X CROSS APPLY
     (SELECT fdata.D.value('.','varchar(50)') AS SplitCust_Id 
      FROM X.xmlfilter.nodes('X') AS fdata(D)) Y
)
,CTE_FINDProductsToMerge AS
( 
    SELECT Distinct Brands,SplitCust_Id 
    FROM CTE 
    WHERE Brands NOT IN (SELECT Brands FROM @t WHERE Rn=1)
)
,CTE2 AS
(
    SELECT c1.Brands, Cust_Id  = STUFF(
            ( SELECT ',' + CAST(SplitCust_Id AS VARCHAR(1000)) 
                FROM CTE_FINDProductsToMerge c2
                WHERE  c1.Brands = c2.Brands
                FOR XML PATH('')),1,1,''
            )
    FROM CTE_FINDProductsToMerge c1
    GROUP BY Brands
)

SELECT Brands,Cust_Id FROM @t  WHERE Brands NOT IN (SELECT Brands From CTE2)
UNION ALL
SELECT Brands,Cust_Id From CTE2
ORDER BY 2

输出:

Brands  Cust_Id
Hitachi 16402, 16407
Hitachi 16409, 16428
JVC 19301, 19308
LG  21001,21007,21008
Panasonic   27909, 27912 
Philips 28501, 28518
Philips 28513, 28516
于 2012-08-29T12:03:57.667 回答
1

如果你想删除重复Brands的,你可以这样做:

SELECT brand, 
(SELECT top 1 cust_ID 
        FROM TopTenBrands 
        WHERE brand = ttb.brand) 
FROM TopTenBrands ttb
GROUP BY brand;

如果你不想使用GROUP BY

SELECT DISTINCT brand, 
(SELECT top 1 cust_ID 
        FROM TopTenBrands 
        WHERE brand = ttb.brand) AS cust_ID
FROM TopTenBrands ttb;

看到这个 SQLFiddle

于 2012-08-29T10:30:43.803 回答
0

您必须首先对concat结果进行分组并将它们插入到临时表中。然后您可以遍历临时表并为每一行执行类似的操作选择不同的分隔符分隔的行并更新该行的临时表。

我不知道你的具体数据模型,但我认为你可以有效地组织你的表模式。

于 2012-08-29T10:09:32.990 回答