可以扩展x量选项的查询&价值组合

时间:2018-02-02 06:21:24

标签: sql sql-server sql-server-2008 sql-server-2012

尝试返回每辆汽车的每个选项值组合的结果,但在理解如何操作时遇到一些麻烦。

这是所涉及的表格的结构和期望的结果。我在网上找到的只是关于使用交叉连接的东西,但在这种情况下,选项维度的数量是未知的,所以这不会像我预期的那样完成。

请参阅下面所需的结构/结果:

enter image description here

我熟悉使用STUFF()FOR XML,因此将它们组合起来不是问题,但问题是找到它将正确返回值以进行定义的查询。

3 个答案:

答案 0 :(得分:0)

检查此查询

with myTable as (
    select
        *
    from
        (values
            ('BMW','COLOR','Black')
            ,('BMW','COLOR','Red')
            ,('BMW','COLOR','Blue')
            ,('BMW','YEAR','2010')
            ,('BMW','YEAR','2011')
            ,('Ferrari','COLOR','Yellow')
            ,('Ferrari','COLOR','White')
            ,('Ferrari','YEAR','2012')
            ,('Ferrari','YEAR','2013')
        ) t(car, name, value)
)

select
    a.car, a.value + ' ' + b.value
from
    myTable a
    join myTable b on a.car = b.car and a.name <> b.name
where
    a.name = 'COLOR'
    and b.name = 'YEAR'

答案 1 :(得分:0)

请试试这个。

Declare @CAR AS TABLE 
    (
     car varchar(100),
     name varchar(100),
     value varchar(100)
    )

    insert into @CAR 
     values
                ('BMW','COLOR','Black')
                ,('BMW','COLOR','Red')
                ,('BMW','COLOR','Blue')
                ,('BMW','YEAR','2010')
                ,('BMW','YEAR','2011')
                ,('Ferrari','COLOR','Yellow')
                ,('Ferrari','COLOR','White')
                ,('Ferrari','YEAR','2012')
                ,('Ferrari','YEAR','2013')

    Select b.car,a.value + ' ' + b.value as value from @CAR b RIGHT OUTER JOIN (
    Select car,name,value as value from @CAR where name='COLOR') as a on a.car = b.car and b.name='YEAR'

答案 2 :(得分:0)

尝试以下查询,在此处使用CTE。我只使用前3或4个CTE来生成主数据,您可以根据表结构更改它。

WITH AUTOMOBILES
AS  (
    SELECT 1 ID,'BMW M3' Name
    UNION
    SELECT 2,'Ferrari F 430'
), OPTIONS
AS  (
    SELECT 1 ID,'Color' Name
    UNION
    SELECT 2,'Year'
    UNION
    SELECT 3,'Top Speed'
),AM_VALUES
AS  (
    SELECT 1 ID,1 AutomobileID,1 OptionID,'Black' Value
    union
    select 2,1,1,'Red'
    union
    select 3,1,1,'Blue'
    union
    select 4,1,2,'2010'
    union
    select 5,1,2,'2011'
    union
    select 6,2,1,'Yellow'
    union
    select 7,2,1,'white'
    union
    select 8,2,2,'2012'
    union
    select 9,2,2,'2013'
    union
    select 10,2,3,'Sp210'
    union
    select 11,2,3,'Sp190'
),INPUT_TABLE
AS  (
SELECT  V.ID,V.AutomobileID,A.Name Car,V.OptionID,O.Name, V.Value
        ,DENSE_RANK() OVER(PARTITION BY V.AutomobileID ORDER BY  V.OptionID ) AS OptionRowNo
FROM    AM_VALUES   AS  V
    INNER JOIN AUTOMOBILES  AS  A   ON  A.ID    =   V.AutomobileID
    INNER JOIN OPTIONS      AS  O   ON  O.ID    =   V.OptionID
),VTREE
AS  (
    SELECT  DISTINCT AutomobileID,ID AS Node,NULL AS Parent,OptionRowNo,CAST(Value AS nvarchar) AS Value,1 RowNo
    FROM    INPUT_TABLE
    WHERE   OptionRowNo     =   1
    UNION ALL

    SELECT  V.AutomobileID,I.ID,V.Node,I.OptionRowNo,CAST(V.Value+' '+I.Value AS nvarchar),RowNo=RowNo+1
    FROM    VTREE   AS  V
        INNER JOIN  INPUT_TABLE AS  I   ON  I.AutomobileID  =   V.AutomobileID
                                        AND I.OptionRowNo   >   V.OptionRowNo
)

SELECT  AutomobileID,Value
FROM    VTREE AS V
WHERE   RowNo =     (   SELECT TOP 1 COUNT(DISTINCT I1.OptionID) 
                        FROM INPUT_TABLE AS I1 
                        WHERE I1.AutomobileID = V.AutomobileID
                        GROUP BY I1.AutomobileID)
ORDER BY AutomobileID,Value

输出: -

AutomobileID    Value
1               Black 2010
1               Black 2011
1               Blue 2010
1               Blue 2011
1               Red 2010
1               Red 2011
2               white 2012 Sp190
2               white 2012 Sp210
2               white 2013 Sp190
2               white 2013 Sp210
2               Yellow 2012 Sp190
2               Yellow 2012 Sp210
2               Yellow 2013 Sp190
2               Yellow 2013 Sp210