小编典典

如何将逗号分隔的列值与另一个表作为行连接

sql

我试图通过首先从成功完成的列“ SupplierId”中转换逗号分隔的值来尝试连接两个表。但是,当我尝试通过外键“
DCLink”连接到另一个带有“供应商”名称的表“供应商”时,就会出现问题。

这就是我的意思:

原始表的选择语句,

  SELECT  InquiryId, SupplierId FROM Procure_InquiryDetails

得到这个结果

InquiryId   SupplierId

1           2,3
2           175
3           170,280
5           
7           12
8           5,9

我可以使用此sql语句从SupplierId拆分列

;WITH CTE
    AS
    (
        SELECT  InquiryId,
                [xml_val] = CAST('<t>' + REPLACE(SupplierId,',','</t><t>') + '</t>' AS XML)
        FROM Procure_InquiryDetails
    )

SELECT  InquiryId,
        [SupplierId] = col.value('.','VARCHAR(100)')
FROM CTE
CROSS APPLY [xml_val].nodes('/t') CA(col)

并得到这些结果

InquiryId   SupplierId
    1           2
    1           3
    2           175
    3           170
    3           280
    5   
    7           12
    8           5
    8           9

但是,当我应用这段代码将InquiryDetails表连接到供应商名称上的供应商表时,

;WITH CTE
AS
(
    SELECT  InquiryId,
            [xml_val] = CAST('<t>' + REPLACE(SupplierId,',','</t><t>') + '</t>' AS XML),
            Vendor.Name
    FROM Procure_InquiryDetails inner join Vendor
    on ',' + Procure_InquiryDetails.SupplierId + ',' like '%,' + cast(Vendor.DCLink as nvarchar(20)) + ',%'
)

SELECT  InquiryId, Name,
        [SupplierId] = col.value('.','VARCHAR(100)')
FROM CTE
CROSS APPLY [xml_val].nodes('/t') CA(col)

这给我带来了非常不便的结果:

InquiryId   Name                                                                                                                                                   SupplierId
----------- ------------------------------------------------------------------------------------------------------------------------------------------------------ ----------------------------------------------------------------------------------------------------
1           Accesskenya Group Ltd                                                                                                                                  2
1           Accesskenya Group Ltd                                                                                                                                  3
1           Aquisana Ltd                                                                                                                                           2
1           Aquisana Ltd                                                                                                                                           3
2           TOYOTA KENYA                                                                                                                                           175
3           Institute of Chartered Shipbrokers ICS-USD                                                                                                             170
3           Institute of Chartered Shipbrokers ICS-USD                                                                                                             280
7           CMA CGM Kenya Ltd                                                                                                                                      12
8           Aon Kenya Insurance Brokers Ltd                                                                                                                        5
8           Aon Kenya Insurance Brokers Ltd                                                                                                                        9
8           Bill investments ltd                                                                                                                                   5
8           Bill investments ltd

我希望join语句能够像原始的select语句一样显示和流动。

我被困住了,似乎无法弄清楚哪里出了问题。任何指向正确方向的指针?


阅读 207

收藏
2021-04-22

共1个答案

小编典典

假设您使用的是SQL Server
2016,string_split()则无需借助CTE或XML方法即可解析CSV列(此外:字段中用逗号分隔的值表示数据模型不佳)。

select I.inquiry_id, sup.value,V.Name
from Procure_InquiryDetails I
CROSS APPLY string_split(I.supplier_value,',') sup
join Vendor v on v.DCLink = sup.value
2021-04-22