[英]SQL: Find all possible combinations/permutations of product option data
我有以下產品數據(用於在線商店):
ProductId ProductOptionGroupId ProductOptionId
26 1 13
26 1 12
44 1 22
44 1 23
44 2 20
44 2 21
44 3 25
44 3 24
其中ProductOptionGroup為(例如)“大小”或“顏色”,而ProductOption為(例如)“大”,“超大”和“紅色”,“黑色”等。
基本上,我想為每個產品找到所有可能的產品選項組合。 例如,對於產品44,我想要:
22, 20, 25 (Large, Black, Cotton)
22, 20, 24 (Large, Black, Nylon)
22, 21, 25 (Large, Red, Cotton)
22, 21, 24 (Large, Red, Nylon)
23, 20, 25 (Extra Large, Black, Cotton)
23, 20, 24 etc...
23, 21, 25
23, 21, 24
每行的每個產品選項組中只有一個產品選項。 即大號和特大號是互斥的。
理想情況下,我希望將每個產品的這些值合並為一個VARCHAR(例如“ 22,21,25”等)。
在SQL Server 2005中如何實現?
謝謝
WITH
data (ProductId, ProductOptionGroupId, ProductOptionId) AS (
/* defining sample data */
SELECT 26, 1, 13 UNION ALL
SELECT 26, 1, 12 UNION ALL
SELECT 44, 1, 22 UNION ALL
SELECT 44, 1, 23 UNION ALL
SELECT 44, 2, 20 UNION ALL
SELECT 44, 2, 21 UNION ALL
SELECT 44, 3, 25 UNION ALL
SELECT 44, 3, 24
),
ranked AS (
/* ranking the group IDs */
SELECT
ProductId,
ProductOptionGroupId,
ProductOptionId,
GroupRank = DENSE_RANK() OVER (PARTITION BY ProductId
ORDER BY ProductOptionGroupId)
FROM data
),
crossjoined AS (
/* obtaining all possible combinations */
SELECT
ProductId,
GroupRank,
ProductVariant = CAST(ProductOptionId AS varchar(250))
FROM ranked
WHERE GroupRank = 1
UNION ALL
SELECT
r.ProductId,
r.GroupRank,
ProductVariant = CAST(c.ProductVariant + ','
+ CAST(r.ProductOptionId AS varchar(10)) AS varchar(250))
FROM ranked r
INNER JOIN crossjoined c ON r.ProductId = c.ProductId
AND r.GroupRank = c.GroupRank + 1
),
maxranks AS (
/* getting the maximum group rank value for every product */
SELECT
ProductId,
MaxRank = MAX(GroupRank)
FROM ranked
GROUP BY ProductId
)
/* getting the max ranked combinations for every product */
SELECT c.ProductId, c.ProductVariant
FROM crossjoined c
INNER JOIN maxranks m ON c.ProductId = m.ProductId
AND c.GroupRank = m.MaxRank
輸出:
ProductId ProductVariant
----------- --------------
26 12
26 13
44 22,20,24
44 22,20,25
44 22,21,24
44 22,21,25
44 23,20,24
44 23,20,25
44 23,21,24
44 23,21,25
有用的讀物:
樣品:
declare @t table(id int, type1 int, type2 int)
insert @t values(1, 1, 1), (1, 1, 2), (1, 2, 1), (2, 2, 1)
select distinct t1.id, t1.type1, t2.type2
from
(
select id, type1
from @t
)t1
full join
(
select id, type2
from @t
)t2 on t2.id = t1.id
輸出:
id type1 type2
----------- ----------- -----------
1 1 1
1 1 2
1 2 1
1 2 2
2 2 1
SQL將取決於您的表結構。 如果列存儲在單獨的表中,則簡單的笛卡爾乘積(無條件聯接)應產生所需的結果。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.