SQL Server使用表中的唯一值组合顺序获取数据

时间:2015-10-05 08:20:55

标签: sql-server stored-procedures

作为我项目的一部分,我需要设计需要以特定格式从Sql数据库表中获取数据的目录树。下面是具有虚拟数据和预期结果的表。你们找到解决方案会很有帮助。

catalog_tree_mstr

id  | catalog_code |    | attribute_id  | level
1     1                    1                 1
2     1                    2                 2
3     1                    3                 6
4     2                    3                 1
5     2                    1                 2

此表' catalog_tree_mstr'包含与目录和属性级别相关联的属性(属性顺序,其中将显示结果)。

列' attribute_id'是表格属性'的参考键。

属性

id   |   attribute_systemname
1           attribute_01
2           attribute_02
3           attribute_03

这个'属性'具有属性名称的表。

dynamic_article_mstr

id  |  article_id   |  attribute_01 |  attribute_02  |  attribute_03
1          1            performance      football        accessories
2          2            performance      football        apparel
3          3            performance      football        apparel
4          4            Performance      basketball      footwear
5          5            Performance      basketball      accessories
7          6            Originals        Football        Apparel
9          8            Originals        basketball      Apparel
10         9            Originals        basketball      accessories
11         10           Originals        basketball      Apparel

这' dynamic_article_mstr'将属性名称作为列的表。

我希望以逗号分隔显示每个唯一的属性值组合,如下所示。

输入参数是' catalog _code' for table' dynamic_article_mstr'。

以下是catalog_code = 1

的必填结果
    1       performance,football,accessories
    2       performance,football,apparel
    3       Performance,basketball,footwear
    4       Performance,basketball,accessories
    5       Originals,Football,Apparel
    6       Originals,basketball,Apparel
    7       Originals,basketball,accessories

以下是catalog_code = 2

的必填结果
    1   accessories,performance
    2   accessories,Originals
    3   apparel,performance
    4   apparel,Originals
    5   footwear,Performance

1 个答案:

答案 0 :(得分:0)

我确定这不是优化的,但此查询应符合您的期望:

DECLARE @CatalogCode INT = 2;

DECLARE @tmp TABLE (id INT, article_id INT, att1 NVARCHAR(30), att2 NVARCHAR(30), att3 NVARCHAR(30))

INSERT INTO @tmp ( id, article_id, att1, att2, att3 )
SELECT  x.id ,
        x.article_id ,
        x.attribute_01 ,
        x.attribute_02 ,
        x.attribute_03
FROM    ( SELECT    dam.id ,
                    dam.article_id ,
                    CASE WHEN EXISTS ( SELECT   1
                                       FROM     dbo.catalog_tree_mstr ctm
                                       WHERE    ctm.catalog_code = @CatalogCode
                                               AND ctm.attribute_id = 1 )
                         THEN dam.attribute_01
                         ELSE NULL
                    END AS attribute_01 ,
                    CASE WHEN EXISTS ( SELECT   1
                                       FROM     dbo.catalog_tree_mstr ctm
                                       WHERE    ctm.catalog_code = @CatalogCode
                                                AND ctm.attribute_id = 2 )
                         THEN dam.attribute_02
                         ELSE NULL
                    END AS attribute_02 ,
                    CASE WHEN EXISTS ( SELECT   1
                                       FROM     dbo.catalog_tree_mstr ctm
                                       WHERE    ctm.catalog_code = @CatalogCode
                                                AND ctm.attribute_id = 3 )
                         THEN dam.attribute_03
                         ELSE NULL
                    END AS attribute_03
          FROM      dbo.dynamic_article_mstr dam
        ) x;

SELECT DISTINCT (SELECT x.att1 + ',' AS [text()]
FROM
(
    SELECT  t.id ,
            t.article_id ,
            t.att1 ,
            (SELECT ctm.level FROM dbo.catalog_tree_mstr ctm WHERE ctm.catalog_code = @CatalogCode AND ctm.attribute_id = 1) AS lvl
    FROM @tmp t
    WHERE t.att1 IS NOT NULL
    UNION ALL
    SELECT  t.id ,
            t.article_id ,
            t.att2 ,
            (SELECT ctm.level FROM dbo.catalog_tree_mstr ctm WHERE ctm.catalog_code = @CatalogCode AND ctm.attribute_id = 2)
    FROM @tmp t
    WHERE t.att2 IS NOT NULL
    UNION ALL
    SELECT  t.id ,
            t.article_id ,
            t.att3 ,
            (SELECT ctm.level FROM dbo.catalog_tree_mstr ctm WHERE ctm.catalog_code = @CatalogCode AND ctm.attribute_id = 3)
    FROM @tmp t
    WHERE t.att3 IS NOT NULL ) x
    WHERE dam.article_id = x.article_id
    ORDER BY x.lvl ASC
    FOR XML PATH('')
    ) AS att
FROM    dbo.dynamic_article_mstr dam