Sql 将行转换为列的透视表
我目前正在运行查询Sql 将行转换为列的透视表,sql,sql-server,pivot,Sql,Sql Server,Pivot,我目前正在运行查询 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
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
我想获得输出(其中\u Comp
,\u Min
和\u Max
与价格属性比较器
,价格属性最小值
和价格属性最大值
)
同样的查询也应该有1
和2
前缀,如4
、5
、19
和32
,或者基于当时表中的内容的任何其他不确定数量的ID
我尝试了一个数据透视表,但我对他们来说是新手,对于如何创建我想要做的事情,我没有第一条线索。使用条件聚合而不是数据透视可能是最简单的方法:
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;
PIVOT函数可能存在的部分问题是由于您有多个要应用该函数的列。如果您想使用PIVOT函数,那么我建议首先取消激活列
PriceAttributeComparator
,PriceAttributeMin
和PriceAttributeMax
。当您取消pivot数据时,您将不再有多个列,您将有多个行,然后您可以将pivot应用于所有适当的值
您没有指定正在使用的SQL Server的版本,但可以将交叉应用与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) |
一旦数据位于多行中,则可以将透视函数应用于列中的值:
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来获得结果,上述版本非常有效:
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;
看。这些解决方案将产生以下结果:
| 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) |
这就是我目前拥有的,并且我希望将其转变为一个轴心,因为在任何给定时间都可以使用AttributeID和属性id的数量vary@bizzehdee . . . 枢轴对这没有帮助。必须在列表中列出用于旋转的属性。您可能需要一个动态SQL解决方案。
| 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) |