gpt4 book ai didi

sql - 如何在字符串中查找项目的并集

转载 作者:行者123 更新时间:2023-12-02 20:56:41 25 4
gpt4 key购买 nike

我有两个表,第一个表
表格1

Customer_id   Item_Id
23 1
23 2
23 1
24 5
24 3
25 4

表2
 Item_Id      Connected_Items
1 2,3,4
2 1,3,6
3 5
4 8,7
5 2,3

我想要的输出是与客户购买的商品关联的所有商品的并集,从关联商品中删除客户购买的所有重复商品和商品
Customer_id    Connected_Items
23 3,4,6
24 2
25 8,7

我要做的是先将Customer_id和Item_Id重复数据删除,然后将其加入表2。
 SELECT
a.customer_id,
b.connected_items
FROM (
SELECT
customer_id,
item_id
FROM
Table 1
GROUP BY
customer_id,
item_id) a
JOIN
Table 2 b
ON
a.Item_id = b.Item_id

我得到的输出是
    23       1,2,3,3,4,6
24 2,3,5
25 8,7

如何从客户已购买的商品中删除与connected_items列表重复的商品?

最佳答案

您可以在sql server中执行以下操作。

with customdata AS (

SELECT
t1.Customer_id ,t2.Item_Id ,t2.Connected_items
FROM table1 AS t1 inner join table2 as t2 on t1.Item_Id = t2.Item_Id
)
,
rowData AS (

SELECT F1.Customer_id,
F1.Item_Id,
O.splitdata
FROM
(
SELECT *,
cast('<X>'+replace(F.Connected_items,',','</X><X>')+'</X>' as XML) as xmlfilter from customdata F
)F1
CROSS APPLY
(
SELECT fdata.D.value('.','varchar(50)') as splitdata
FROM f1.xmlfilter.nodes('X') as fdata(D)) O
),
UniqueSet AS
(

SELECT
rd.Customer_id,rd.Item_Id
FROM rowData AS rd
GROUP BY rd.Customer_id,rd.Item_Id
),
FinalSet AS
(

SELECT
DISTINCT aa.Customer_id,aa.splitdata
FROM rowData AS aa
WHERE NOT EXISTS (SELECT * FROM UniqueSet as us WHERE us.Customer_id = aa.Customer_id AND us.Item_Id = aa.splitdata )
)



SELECT DISTINCT p.Customer_id,
STUFF((SELECT distinct ',' + p1.splitdata
FROM FinalSet p1
WHERE p.Customer_id = p1.Customer_id
FOR XML PATH(''), TYPE
).value('.', 'NVARCHAR(MAX)')
,1,1,'') Color
FROM FinalSet p;

关于sql - 如何在字符串中查找项目的并集,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44295221/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com