简体   繁体   English

SQL数据透视表不起作用

[英]SQL Pivot Table isn't working

SQL 2005 SQL 2005

I have a temp table: 我有一个临时表:

 Year         PercentMale   PercentFemale  PercentHmlss   PercentEmployed  TotalSrvd
 2008           100                0           0              100              1
 2009           55                40           0               80             20
 2010           64                35           0               67            162
 2011           69                27           0               34            285
 2012           56                43          10                1             58

and I want to create a query to display the data like this: 我想创建一个查询以显示如下数据:

                    2008    2009    2010    2011    2012
 PercentMale         100     55      64      69      56 
 PercentFemale        -      40      35      27      43 
 PercentHmlss         -      -       -       -       10 
 PercentEmployed     100     80      67      34      1 
 TotalSrvd            1      20     162     285      58 

Can I use a pivot table to accomplish this? 我可以使用数据透视表来完成此操作吗? If so, how? 如果是这样,怎么办? I've tried using a pivot but have found no success. 我尝试使用枢轴,但没有成功。

 select PercentHmlss,PercentMale,Percentfemale,
     PercentEmployed,[2008],[2009],[2010],[2011],[2012] from 

 (select PercentHmlss,PercentMale, Percentfemale, PercentEmployed,
     TotalSrvd,year from @TempTable)as T

  pivot (sum (TotalSrvd) for year 
     in ([2008],[2009],[2010],[2011],[2012])) as pvt

This is the result: 结果如下:

 PercentHmlss   PercentMale     Percentfemale PercentEmployed [2008]  [2009]    [2010]      [2011]   [2012]
    0               55              40            80           NULL     20      NULL         NULL     NULL
    0               64              35            67           NULL    NULL     162            NULL  NULL
    0               69              27            34           NULL    NULL     NULL          285     NULL
    0              100               0           100             1     NULL     NULL         NULL    NULL
   10               56              43             1           NULL    NULL     NULL         NULL     58

Thanks. 谢谢。

For this to work you will want to perform an UNPIVOT and then a PIVOT 为此,您需要先执行UNPIVOT ,然后执行PIVOT

SELECT *
from
(
  select year, quantity, type
  from 
  (
    select year, percentmale, percentfemale, percenthmlss, percentemployed, totalsrvd
    from t
  ) x
  UNPIVOT 
  (
    quantity for type
    in 
    ([percentmale]
     , [percentfemale]
     , [percenthmlss]
     , [percentemployed]
     , [totalsrvd])
  ) u
) x1
pivot
(
  sum(quantity)
  for Year in ([2008], [2009], [2010], [2011], [2012])
) p

See a SQL Fiddle with a Demo 查看带有演示SQL Fiddle

Edit Further explanation: 编辑进一步的解释:

You were close with your PIVOT query that you tried, in that you got the data for the Year in the column format that you wanted. 您已与尝试的PIVOT查询保持紧密联系,因为您以所需的列格式获得了Year的数据。 However, since you want the data that was contained in the columns initially percentmale , percentfemale , etc in the row of data - you need to unpivot the data first. 不过,既然你想这是包含在最初的列中的数据percentmalepercentfemale等数据的行-你需要首先unpivot的数据。

Basically, what you are doing is taking the original data and placing it all in rows based on the year. 基本上,您要做的是获取原始数据并将其全部基于年份放置在行中。 The UNPIVOT is going to place your data in the format ( Demo ): UNPIVOT将以( Demo )格式放置数据:

Year    Quantity    Type
2008    100         percentmale
2008    0           percentfemale
etc

Once you have transformed the data into this format, then you can perform the PIVOT to get the result you want. 将数据转换为这种格式后,即可执行PIVOT以获取所需的结果。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM