-1

我目前运行查询

SELECT [PriceAttributeID]
  ,[PriceID]
  ,[AttributeID]
  ,[PriceAttributeComparator]
  ,[PriceAttributeMin]
  ,[PriceAttributeMax]
FROM [PriceAttribute]

这给出了输出

1   2   1   1   S   NULL
2   3   1   1   M   NULL
3   4   1   1   L   NULL
4   5   1   1   L   NULL
5   5   2   1   Black   NULL

我想得到输出(在哪里_Comp_Min_Max相关PriceAttributeComparatorPriceAttributeMinPriceAttributeMax

PriceID    1_Comp    1_Min    1_Max    2_Comp    2_Min    2_Max
      2         1        S     NULL      NULL     NULL     NULL
      3         1        M     NULL      NULL     NULL     NULL
      4         1        L     NULL      NULL     NULL     NULL
      5         1        L     NULL         1    Black     NULL

根据当时表中的内容,同样的查询也应该具有前缀为、1_和或任何其他不确定数量的 ID。2_4_5_19_32_

我尝试了一个 PIVOT 表,但我对他们来说是新手,并且没有关于如何创建我想要做的事情的第一个线索。

4

2 回答 2

1

您可能在使用 PIVOT 函数时遇到的部分问题是由于您有多个要应用该函数的列。如果您想使用 PIVOT 功能,那么我建议首先取消旋转列PriceAttributeComparator,PriceAttributeMinPriceAttributeMax. 当您取消透视数据时,您将不再有多个列,您将拥有多行,然后您可以将透视应用于所有适当的值。

您没有指定您使用的 SQL Server 版本,但您可以使用 CROSS APPLY 和 UNION ALL 来取消透视列:

select priceid, 
  col = cast(attributeid as varchar(10))+'_'+ col, 
  value
from 
(
  select PriceID, 
    AttributeID, 
    comp = cast(PriceAttributeComparator as varchar(10)),
    [min] = cast(PriceAttributeMin as varchar(10)), 
    [max] = cast(PriceAttributeMax as varchar(10))
  from PriceAttribute
) d
cross apply
(
  select 'comp', comp union all
  select 'min', [min] union all
  select 'max', [max] 
) c (col, value)

请参阅演示。此过程会将您的数据转换为以下格式:

| PRICEID |    COL |  VALUE |
-----------------------------
|       2 | 1_comp |      1 |
|       2 |  1_min |      S |
|       2 |  1_max | (null) |
|       3 | 1_comp |      1 |
|       3 |  1_min |      M |
|       3 |  1_max | (null) |

一旦数据位于多行中,您就可以将 PIVOT 函数应用于以下值col

select priceid,
  [1_comp], [1_min], [1_max], [2_comp], [2_min], [2_max]
from
(
  select priceid, 
    col = cast(attributeid as varchar(10))+'_'+ col, 
    value
  from 
  (
    select PriceID, 
      AttributeID, 
      comp = cast(PriceAttributeComparator as varchar(10)),
      [min] = cast(PriceAttributeMin as varchar(10)), 
      [max] = cast(PriceAttributeMax as varchar(10))
    from PriceAttribute
  ) d
  cross apply
  (
    select 'comp', comp union all
    select 'min', [min] union all
    select 'max', [max] 
  ) c (col, value)
) src
pivot
(
  max(value)
  for col in ([1_comp], [1_min], [1_max], [2_comp], [2_min], [2_max])
) piv;

请参阅SQL Fiddle with Demo

如果您有已知数量的值,则上述版本效果很好,但如果值未知,则需要使用动态 SQL 来获取结果:

DECLARE @cols AS NVARCHAR(MAX),
    @query  AS NVARCHAR(MAX)

select @cols = STUFF((SELECT ',' + QUOTENAME(cast(attributeid as varchar(10))+'_'+ col) 
                    from
                    (
                      select distinct attributeid
                      from priceattribute
                    ) d
                    cross apply
                    (
                      select 'comp', 1 union all
                      select 'min', 2 union all
                      select 'max', 3 
                    ) c (col, so)
                    group by attributeid, col, so
                    order by attributeid, so
            FOR XML PATH(''), TYPE
            ).value('.', 'NVARCHAR(MAX)') 
        ,1,1,'')

set @query = 'SELECT priceid, ' + @cols + ' 
            from 
            (
                select priceid, 
                  col = cast(attributeid as varchar(10))+''_''+ col, 
                  value
                from 
                (
                  select PriceID, 
                    AttributeID, 
                    comp = cast(PriceAttributeComparator as varchar(10)),
                    [min] = cast(PriceAttributeMin as varchar(10)), 
                    [max] = cast(PriceAttributeMax as varchar(10))
                  from PriceAttribute
                ) d
                cross apply
                (
                  select ''comp'', comp union all
                  select ''min'', [min] union all
                  select ''max'', [max] 
                ) c (col, value)
            ) x
            pivot 
            (
                max(value)
                for col in (' + @cols + ')
            ) p '

execute sp_executesql @query;

请参阅SQL Fiddle with Demo。这些解决方案将给出结果:

| PRICEID | 1_COMP | 1_MIN |  1_MAX | 2_COMP |  2_MIN |  2_MAX |
----------------------------------------------------------------
|       2 |      1 |     S | (null) | (null) | (null) | (null) |
|       3 |      1 |     M | (null) | (null) | (null) | (null) |
|       4 |      1 |     L | (null) | (null) | (null) | (null) |
|       5 |      1 |     L | (null) |      1 |  Black | (null) |
于 2013-07-28T17:05:50.143 回答
0

使用条件聚合而不是数据透视可能是最简单的:

SELECT PriceID,
       max(case when AttributeID = 1 then PriceAttributeComparator end) as comp_1,
       max(case when AttributeID = 1 then PriceAttributeMin end) as min_1,
       max(case when AttributeID = 1 then PriceAttributeMax end) as max_1,
       max(case when AttributeID = 2 then PriceAttributeComparator end) as comp_2,
       max(case when AttributeID = 2 then PriceAttributeMin end) as min_2,
       max(case when AttributeID = 2 then PriceAttributeMax end) as max_2
FROM PriceAttribute pa
group by PriceId;
于 2013-07-28T14:07:38.150 回答