使用SQL Server创建所有可能的组合

时间:2013-09-06 13:07:47

标签: sql sql-server join

我在这里发现了类似的问题,但要么我得不到答案,要么他们不适用......这就是我需要的,并认为这很简单: 我有一组项目,每个项目都有一组子项目。每个项目的子项目数量会发生变化。 E.G:

Item 1
   SubItem 1-1
   SubItem 1-2
   SubItem 1-3
Item 2
   SubItem 2-1
Item 3
   SubItem 3-1
   SubItem 3-2

对于一个非常具体的用途,想要的是为每个项目的子项目的每个可能组合添加注释,加上每个子项目的布尔属性,所以它最终如下:

Item 1   Subitem 1-1 = True, Subitem 1-2 = True, Subitem 1-3 = True
Item 1   Subitem 1-1 = True, Subitem 1-2 = True, Subitem 1-3 = False
Item 1   Subitem 1-1 = True, Subitem 1-2 = False, Subitem 1-3 = True
Item 1   Subitem 1-1 = True, Subitem 1-2 = False, Subitem 1-3 = False
Item 1   Subitem 1-1 = False, Subitem 1-2 = True, Subitem 1-3 = True
... (the rest of Item 1 possible combinations)
Item 2   Subitem 2-1 = True
Item 2   Subitem 2-1 = False
Item 3   Subitem 3-1 = True, Subitem 3-2 = True
Item 3   Subitem 3-1 = True, Subitem 3-2 = False
Item 3   Subitem 3-1 = False, Subitem 3-2 = True
Item 3   Subitem 3-1 = False, Subitem 3-2 = False

我尝试了内部连接和交叉连接的各种各样但无法使其工作。我认为布尔部分可以使用交叉连接添加到具有值为True和False的两行的表中,并且我还认为我需要执行“FOR XML”子查询以将子项目放在一行中,但是我没有得到子项目组合

这是我到目前为止所做的:

-- Schema creation and data filling
DECLARE @Item TABLE (ItemId int, Name varchar(50))
DECLARE @Item_SubItem TABLE (ItemId int, SubitemId int)
DECLARE @SubItem TABLE (SubitemId int, Name varchar(50))

INSERT INTO @Item values (1, 'Item 1')
INSERT INTO @Item values (2, 'Item 2')
INSERT INTO @Item values (3, 'Item 3')
INSERT INTO @SubItem values (1, 'SubItem 1-1')
INSERT INTO @SubItem values (2, 'SubItem 1-2')
INSERT INTO @SubItem values (3, 'SubItem 1-3')
INSERT INTO @SubItem values (4, 'SubItem 2-1')
INSERT INTO @SubItem values (5, 'SubItem 3-1')
INSERT INTO @SubItem values (6, 'SubItem 3-2')
INSERT INTO @Item_SubItem values (1, 1)
INSERT INTO @Item_SubItem values (1, 2)
INSERT INTO @Item_SubItem values (1, 3)
INSERT INTO @Item_SubItem values (2, 4)
INSERT INTO @Item_SubItem values (3, 5)
INSERT INTO @Item_SubItem values (3, 6)

select I.Name, SI.Name
  from @Item I
       inner join @Item_SubItem ISI on ISI.ItemId = I.ItemId
       INNER JOIN @SubItem SI on SI.SubitemId = ISI.SubitemId
 order by I.Name, SI.Name


-- Actual query
SELECT ItemName = M.name, (SELECT iC.name + '=' + CASE AuxCode WHEN 1 THEN 'True' WHEN 0 THEN 'False' END + ' '
                                   FROM Item_subitem AS iCGM
                                        INNER JOIN Subitem AS iC ON iC.SubitemId = iCGM.SubitemId
                                        CROSS JOIN (SELECT AuxCode = 1 UNION SELECT AuxCode = 0) Aux
                                  WHERE iCGM.ItemId = M.ItemId
                                  ORDER BY iC.name
                                    FOR XML PATH(''))
  FROM Item M

所以,这是子查询对我来说失败了。任何帮助将不胜感激!

1 个答案:

答案 0 :(得分:3)

以下是如何最多达到4个级别(您的样本数据只需要3个,但我希望确保此功能超出此范围)。你应该能够按照这个模式来达到7,10,你有什么。哦,不要指望这很快。

;WITH z AS 
(
  SELECT i,inm,si,snm,truth,c FROM 
  (
    SELECT i = i.ItemId, inm = i.Name, si = isi.SubItemId, snm = s.Name,
      c = COUNT(isi.SubItemId) OVER (PARTITION BY i.ItemId)
    FROM @Item_SubItem AS isi
    INNER JOIN @Item AS i    ON isi.ItemId    = i.ItemId
    INNER JOIN @SubItem AS s ON isi.SubItemId = s.SubItemId
  ) AS y
  CROSS JOIN (VALUES('true'),('false')) AS t(truth)
)
SELECT Item = z1.inm, 
  SubItems = COALESCE(       z1.snm + ' = ' + z1.truth,'')
           + COALESCE(', ' + z2.snm + ' = ' + z2.truth,'')
           + COALESCE(', ' + z3.snm + ' = ' + z3.truth,'')
           + COALESCE(', ' + z4.snm + ' = ' + z4.truth,'')
FROM z AS z1
  LEFT OUTER JOIN z AS z2 
    ON z1.i = z2.i AND z1.si < z2.si
  LEFT OUTER JOIN z AS z3 
    ON z2.i = z3.i AND z2.si < z3.si 
  LEFT OUTER JOIN z AS z4 
    ON z3.i = z4.i AND z3.si < z4.si 
  WHERE (z1.c = 1) 
    OR (z1.c = 2 AND z2.i IS NOT NULL)
    OR (z1.c = 3 AND z3.i IS NOT NULL)
    OR (z1.c = 4 AND z4.i IS NOT NULL);

给出样本数据的结果:

Item        SubItems
------      ---------------------------------------------------------------
Item 1      SubItem 1-1 = true, SubItem 1-2 = true, SubItem 1-3 = true
Item 1      SubItem 1-1 = true, SubItem 1-2 = true, SubItem 1-3 = false
Item 1      SubItem 1-1 = true, SubItem 1-2 = false, SubItem 1-3 = true
Item 1      SubItem 1-1 = true, SubItem 1-2 = false, SubItem 1-3 = false
Item 1      SubItem 1-1 = false, SubItem 1-2 = true, SubItem 1-3 = true
Item 1      SubItem 1-1 = false, SubItem 1-2 = true, SubItem 1-3 = false
Item 1      SubItem 1-1 = false, SubItem 1-2 = false, SubItem 1-3 = true
Item 1      SubItem 1-1 = false, SubItem 1-2 = false, SubItem 1-3 = false
Item 2      SubItem 2-1 = true
Item 2      SubItem 2-1 = false
Item 3      SubItem 3-1 = true, SubItem 3-2 = true
Item 3      SubItem 3-1 = true, SubItem 3-2 = false
Item 3      SubItem 3-1 = false, SubItem 3-2 = true
Item 3      SubItem 3-1 = false, SubItem 3-2 = false
在考虑了这一点之后

编辑,我测试了这个比较首先将一堆信息转储到#temp表,这似乎更好地优化,尽管订单不同(仍按ItemId排序,但错误值排序更高):

SELECT c.i, c.inm, c.si, c.snm, c.c, t.truth 
INTO #x
FROM 
(
  SELECT 
     i = i.ItemId, inm = i.Name, si = isi.SubItemId, snm = s.Name,
     c = COUNT(isi.SubItemId) OVER (PARTITION BY i.ItemId)
  FROM @Item_SubItem AS isi
  INNER JOIN @Item AS i    ON isi.ItemId    = i.ItemId
  INNER JOIN @SubItem AS s ON isi.SubItemId = s.SubItemId
) AS c
CROSS JOIN (VALUES('true'),('false')) AS t(truth);

CREATE UNIQUE CLUSTERED INDEX x ON #x(i,si,truth);

SELECT 
  Item = z1.inm, 
  SubItems = COALESCE(       z1.snm + ' = ' + z1.truth,'')
           + COALESCE(', ' + z2.snm + ' = ' + z2.truth,'')
           + COALESCE(', ' + z3.snm + ' = ' + z3.truth,'')
           + COALESCE(', ' + z4.snm + ' = ' + z4.truth,'')
FROM #x AS z1
  LEFT OUTER JOIN #x AS z2 ON z1.i = z2.i AND z1.si < z2.si
  LEFT OUTER JOIN #x AS z3 ON z2.i = z3.i AND z2.si < z3.si
  LEFT OUTER JOIN #x AS z4 ON z3.i = z4.i AND z3.si < z4.si
WHERE (z1.c = 1) 
  OR (z1.c = 2 AND z2.i IS NOT NULL)
  OR (z1.c = 3 AND z3.i IS NOT NULL)
  OR (z1.c = 4 AND z4.i IS NOT NULL);

DROP TABLE #x;

如果基础表被索引,原始版本会更有利,例如

DECLARE @Item TABLE (ItemId int PRIMARY KEY, Name varchar(50));

DECLARE @Item_SubItem TABLE (ItemId int, SubitemId int, 
  PRIMARY KEY (ItemId,SubItemId));

DECLARE @SubItem TABLE (SubitemId int PRIMARY KEY, Name varchar(50));

您应该根据实际数据/架构测试这两种变体。