1

可能重复:
在 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
4

1 回答 1

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 小提琴演示


但这很难看,因为您必须编写这些标题列表。但是,最好通过动态获取标题列表来动态执行此操作,如下所示:

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 小提琴演示

这应该给你:

| 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
于 2012-12-10T08:42:09.903 回答