1

我有一个 SELECT 语句,它从几个 JOINS 中收集数据,从而产生:

Site    Description Status
  1     Project 1   Not Started
  1     Project 5   In Progress
  1     Project 2   Testing
  1     Project 12  Complete
  1     Project 2   Not Started
  2     Project 13  Not Started
  2     Project 20  Testing
  2     Project 21  In Progress
  2     Project 22  In Progress
  2     Project 23  Testing
  3     Project 24  Not Started
  3     Project 25  Not Started
  3     Project 26  Testing
  3     Project 29  Complete

我想总结每个站点的状态以产生:

Site    Not Started In Progress Testing Complete
   1        2              1       1       1
   2        1              2       2       0
   3        2              0       1       1

我应该能够使用“WITH”子句来处理输出。目前,它看起来像这样:

WITH SiteData AS (SELECT data from multiple JOINS...)

SELECT Site, Description, Status FROM SiteData

因此,我不想仅仅显示数据列,而是将其转换为按站点划分的各种状态中的每个项目的计数。

那有意义吗?

4

1 回答 1

4

这种类型的数据转换称为数据透视。有几种方法可以将行数据转换为列。

您可以使用带有 a 的聚合函数CASE

select site,
  sum(case when Status = 'Not Started' then 1 else 0 end) [Not Started],
  sum(case when Status = 'In Progress' then 1 else 0 end) [In Progress],
  sum(case when Status = 'Testing' then 1 else 0 end) Testing,
  sum(case when Status = 'Complete' then 1 else 0 end) Complete
from sitedata
group by site

请参阅SQL Fiddle with Demo

您将替换为sitedata现有查询。

或者您可以使用以下PIVOT功能:

select site,
  [Not Started], 
  [In Progress], 
  Testing, 
  Complete
from
(
  select [Site], Status
  from sitedata
) src
pivot
(
  count(Status)
  for Status in ([Not Started], [In Progress], Testing, Complete)
) piv

请参阅SQL Fiddle with Demo

如果您有未知数量的Status值,则可以使用动态 SQL:

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

select @cols = STUFF((SELECT distinct ',' + QUOTENAME([Status]) 
                    from SiteData
            FOR XML PATH(''), TYPE
            ).value('.', 'NVARCHAR(MAX)') 
        ,1,1,'')

set @query = 'SELECT site, ' + @cols + ' from 
             (
                select [Site], Status
                from sitedata
            ) x
            pivot 
            (
                count(Status)
                for Status in (' + @cols + ')
            ) p '

execute(@query)

请参阅SQL Fiddle with Demo

每个查询的结果都是一样的:

| SITE | NOT STARTED | IN PROGRESS | TESTING | COMPLETE |
---------------------------------------------------------
|    1 |           2 |           1 |       1 |        1 |
|    2 |           1 |           2 |       2 |        0 |
|    3 |           2 |           0 |       1 |        1 |

编辑#1,如果你想要Total一行,那么你可以使用:

select 
  case when site is not null then cast(site as varchar(10)) else 'Total' end Site,
  sum([Not Started]) [Not Started], 
  Sum([In Progress])[In Progress], 
  Sum(Testing) Testing, 
  sum(Complete) Complete,
  Sum([Not Started]+[In Progress]+ Testing+ Complete) Total
from
(
  select [Site], Status
  from sitedata
) src
pivot
(
  count(Status)
  for Status in ([Not Started], [In Progress], Testing, Complete)
) piv
group by rollup (site)

请参阅带有演示的 SQL Fiddle

于 2013-02-25T19:17:35.173 回答