如何使用SQL Pivot表转换数据

时间:2016-02-24 13:26:06

标签: sql-server

我需要使用数据透视表(动态列)将下面提到的表行转换为列,其中表文章编号,文章名称,目录代码是固定列。您的帮助将受到高度赞赏

ArticleNumber   ArticleName CatalogCode AttributeName   AttributeValue
s123              abc         1          SportsCategory     Rakesh
s123              abc         1          Brand              Amar
s123              abc         1          ProductDivision    Ball
s123              abc         1          MarketingDivision  Boss
s222              XYZ         1          SportsCategory     Rakesh5
s222              XYZ         1          Brand              Red
s222              XYZ         1          ProductDivision    blue
s222              XYZ         1          SportsCategory     Rakesh8

输出:

ArticleNumber   ArticleName CatalogCode SportsCategory  Brand   ProductDivision MarketingDivision
s123             abc          1          Rakesh         Amar     Ball           Boss
s222             abc          1          Rakesh5        Red      blue           Rakesh8

2 个答案:

答案 0 :(得分:1)

SELECT *
FROM
(
  SELECT ARTICLE, NAME, CODE, ATTRIBUTENAME, ATTRIBUTEVALUE
  FROM #TABLE1
) D
PIVOT
(
  MAX(ATTRIBUTEVALUE)
  FOR ATTRIBUTENAME IN  ([SPORTSCATEGORY],[BRAND],[PRODUCTDIVISION],[MARKETINGDIVISION])
) PIV;``

答案 1 :(得分:0)

这是一个简单的支点

mPreviewSession2.setRepeatingBurst(mPreviewBuilder2, null, mBackgroundHandler);