我按照 SQL count the number of comma-separated string match in another string 中的示例进行操作,并将其应用到您的示例中。
CREATE TABLE Example
(
Basketld INT,
OrderNo VARCHAR(512),
BasketSKUs VARCHAR(512),
OrderSKUs VARCHAR(512)
);
INSERT INTO Example (Basketld, OrderNo, BasketSKUs, OrderSKUs) VALUES
(20009610, 'MFTJZ3', '064008, 064011, 064057, 064074, 064075, 064092, 064093, 064094, 064095, 06...', '226374, 410990, 561242, 569197, 630684, 670071, 685224, 685225, 685226, 706226, 706227, 706228, 706229, 706230, 706231, 706232'),
(20011137, 'MFTJZ3', '856354, 857133, 857134, 857135, 857511, 857512, 857513, 857530, 857646, 85...', '226374, 410990, 561242, 569197, 630684, 670071, 685224, 685225, 685226, 706226, 706227, 706228, 706229, 706230, 706231, 706232'),
(20011138, 'MFTJZ3', '055522, 055523, 055524, 055525, 089893, 112935, 119355, 634691, 634692, 65...', '226374, 410990, 561242, 569197, 630684, 670071, 685224, 685225, 685226, 706226, 706227, 706228, 706229, 706230, 706231, 706232');
SELECT * FROM Example
;with Baskets as
(
select Basketld,OrderNo, Value = trim(b.value)
from Example a
cross apply string_split(a.BasketSKUs, ',') b
), Orders as
(
select Basketld,OrderNo, Value = trim(b.value)
from Example a
cross apply string_split(a.OrderSKUs, ',') b
)
select
b.Basketld,
ItemCount = count(1),
MatchCount = count(o.value)
from Baskets b
left outer join Orders o
on b.Basketld = o.Basketld
and b.Value = o.Value
group by b.Basketld
篮子 | 项目计数 | 匹配数 |
---|---|---|
20009610 | 10 | 0 |
20011137 | 10 | 0 |
20011138 | 10 | 0 |