透视Sql server
我有一个varchar类型列的表。如何旋转它们 我的桌子最初看起来像透视Sql server,sql,sql-server,Sql,Sql Server,我有一个varchar类型列的表。如何旋转它们 我的桌子最初看起来像 Product Id Name Value P1 Version Type v1 P1 License Type POS P1 Product Description This is P1 我想转置它来产生以下结果 Product Id Version Type License Ty
Product Id Name Value
P1 Version Type v1
P1 License Type POS
P1 Product Description This is P1
我想转置它来产生以下结果
Product Id Version Type License Type Product Description
P1 v1 POS This is P1
您可以使用下面提到的任何方法来获得所需的结果
Select ProductID,
Min(Case Name When 'Version Type' Then Value End) [Version Type],
Min(Case Name When 'License Type' Then Value End) [License Type],
Min(Case Name When 'Product Description' Then Value End) [Product Description]
From <TABLE NAME>
Group By ProductID
如果我们想在pivot查询中包含所有产品,最好使用:FROM选择DISTINCT ProductId FROM table t1。如果不是的话,你的建议是个好主意。
Select ProductID,
Min(Case Name When 'Version Type' Then Value End) [Version Type],
Min(Case Name When 'License Type' Then Value End) [License Type],
Min(Case Name When 'Product Description' Then Value End) [Product Description]
From <TABLE NAME>
Group By ProductID
select * from (select ProductID, name, value from <TABLENAME>) as tbl
Pivot
(
max(value)
for name in ([Version Type], [License Type], [Product Description])
) as p