如何以数据透视表格形式获得以下查询的结果?

时间:2012-12-10 08:23:44

标签: sql sql-server sql-server-2008 pivot

  

可能重复:
  Pivot data in T-SQL

请在查询中进行必要的更改以创建数据透视表单显示...

select COUNT(employeeid) as count_of_employees,Title 
from HumanResources.Employee 
group by Title order by no_of_employees desc

此查询返回以下结果

count_of_employees  Title
26                  Production Technician - WC50
26                  Production Technician - WC60
26                  Production Technician - WC40
25                  Production Technician - WC30
22                  Production Technician - WC20
17                  Production Technician - WC10
15                  Production Technician - WC45
14                  Sales Representative
9                   Buyer
5                   Marketing Specialist
4                   Scheduling Assistant

我需要所需的结果

Production Technician-WC50 |  Production Technician-WC60  |   Production Technician-WC40
           26              |            26                |                26

1 个答案:

答案 0 :(得分:3)

要按照您描述的方式将行旋转到列中,您必须使用PIVOT表运算符。像这样:

SELECT *
FROM
(
  SELECT [count_of_employees], [Title]
  FROM YourQuery
  ) t
PIVOT
(
  MAX(count_of_employees)
  FOR Title IN([Production Technician - WC50], 
               [Production Technician - WC60], 
               [Production Technician - WC40],
               [Production Technician - WC30], 
               [Production Technician - WC20], 
               [Production Technician - WC10], 
               [Production Technician - WC45], 
               [Sales Representative], 
               [Buyer], 
               [Marketing Specialist], 
               [Scheduling Assistant])
 ) p;

SQL Fiddle Demo


但这很难看,因为你必须写下这些标题清单。但是,最好动态地执行此操作,方法是动态获取标题列表:

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

SELECT @cols = STUFF((SELECT distinct ',' +  QUOTENAME (title)
               FROM YourQuery
               FOR XML PATH(''), TYPE
            ).value('.', 'NVARCHAR(MAX)')  ,1,1,'');

SET @query = 'SELECT ' + @cols + ' FROM
(
   SELECT [count_of_employees], [Title] FROM YourQuery
) t
PIVOT
(
   MAX(count_of_employees)
   FOR title IN (' + @cols + ')
             ) p ';

EXECUTE(@query);

SQL Fiddle Demo

这应该给你:

| BUYER | MARKETING SPECIALIST | PRODUCTION TECHNICIAN - WC10 | PRODUCTION TECHNICIAN - WC20 | PRODUCTION TECHNICIAN - WC30 | PRODUCTION TECHNICIAN - WC40 | PRODUCTION TECHNICIAN - WC45 | PRODUCTION TECHNICIAN - WC50 | PRODUCTION TECHNICIAN - WC60 | SALES REPRESENTATIVE | SCHEDULING ASSISTANT |
-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
|     9 |                    5 |                           17 |                           22 |                           25 |                           26 |                           15 |                           26 |                           26 |                   14 |                    4 |

这样更好,但您可能需要小心动态SQL,请了解更多详情:


请注意:我在此处的答案中使用的表YourQuery必须替换为您的查询:

select COUNT(employeeid) as count_of_employees,Title 
from HumanResources.Employee 
group by Title order by no_of_employees desc