[英]How to join comma separated column values with another table as rows
I am trying to join two tables by first converting a comma separated values from a column "SupplierId" which I am doing successfully. 我试图通过首先从我正在成功执行的“SupplierId”列转换逗号分隔值来加入两个表。 However, the issue comes in when I try joining to another table 'Vendors' with the Supplier names via a foreign key 'DCLink'.
但是,当我尝试通过外键“DCLink”连接到具有供应商名称的另一个表“供应商”时,问题就出现了。
This is what I mean: 这就是我的意思:
The select statement for the Original Table, 原始表的select语句,
SELECT InquiryId, SupplierId FROM Procure_InquiryDetails
Gives this result 给出了这个结果
InquiryId SupplierId
1 2,3
2 175
3 170,280
5
7 12
8 5,9
I am able to split the columns from SupplierId using this sql statement 我可以使用此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)
and get these results 并获得这些结果
InquiryId SupplierId
1 2
1 3
2 175
3 170
3 280
5
7 12
8 5
8 9
When I apply this bit of code to join the InquiryDetails table to the Vendor Table on supplier Name however, 当我应用这段代码将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)
It gives me this very inconvenient result: 它给了我这个非常不方便的结果:
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
I would wish for the join statement to show and flow as the original select statement. 我希望join语句显示和流动为原始select语句。
I am stuck and I cannot seem to figure out where I am going wrong. 我被困住了,我似乎无法弄清楚我哪里出错了。 Any pointers in the right direction?
任何正确方向的指针?
You've forgotten to supply expected results, so this is a stab in the dark, however, what's wrong with splitting your string and using the results with a JOIN
: 你忘了提供预期的结果,所以这是一个黑暗的刺,然而,分裂你的字符串和使用
JOIN
结果是错误的:
SELECT {Needed Columns}
FROM dbo.Procure_InquiryDetails PID
CROSS APPLY STRING_SPLIT(PID.SupplierId,',') SS
JOIN dbo.Vendor V ON SS.[value] = V.SupplierID;
Ideally, however, you shouldn't be storing delimited data in your RDBMS. 但是,理想情况下,您不应该在RDBMS中存储分隔数据。 Considering switching to a proper normalised many-to-many relationship structure.
考虑切换到适当的规范化多对多关系结构。
If you're still on SQL Server 2008 (to which I would highly recommend you upgrade), then you can use delimitedsplit8k , or on 2012/2014 you can use delimitedsplit8k_lead . 如果你还在SQL Server 2008上(我强烈建议你升级),那么你可以使用delimitedsplit8k ,或者在2012/2014你可以使用delimitedsplit8k_lead 。
Assuming you're using SQL Server 2016, you can use string_split()
to parse out your CSV column (aside: comma-separated values in a field is a sign of a poor data model) without resorting to a CTE or XML methods. 假设您正在使用SQL Server 2016,您可以使用
string_split()
来解析您的CSV列(除了:字段中逗号分隔的值是不良数据模型的标志),而无需借助CTE或XML方法。
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
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.