7

我对 SQL 相当陌生,但相信我在发布此内容之前已经寻求帮助。

我有一个查询,它返回分配给工作的人员列表,这些工作也有不同的长度,分配给这些工作的人工作的长度也不同。

我想要做的是转换什么是类似记录的列表,唯一的变量变化是日期,以及如何旋转这些数据,以便日期成为列标题,行表示 BOOL 是/否。

这是我目前正在取回的数据。JSON编码

{"results":[{"role":"Vision Supervisor","familyname":"Unsworth","givenname":"Simon","skill":"10","level":"Telegenic Staff","id":"664","date":"2013-03-27"},{"role":"Vision Supervisor","familyname":"Unsworth","givenname":"Simon","skill":"10","level":"Telegenic Staff","id":"664","date":"2013-03-26"},{"role":"Vision Supervisor","familyname":"Unsworth","givenname":"Simon","skill":"10","level":"Telegenic Staff","id":"664","date":"2013-03-25"},{"role":"Vision Supervisor","familyname":"Unsworth","givenname":"Simon","skill":"10","level":"Telegenic Staff","id":"664","date":"2013-03-24"}]}

我想回来的是:

{"results":[{"role":"Vision Supervisor","familyname":"Unsworth","givenname":"Simon","skill":"10","level":"Telegenic Staff","id":"664","2013-03-27":"YES","2013-03-26":"YES","2013-03-25":"YES","2013-03-24":"YES"}]}

我确定这是某种 PIVOT 查询,但我无法让它工作。

谢谢

4

1 回答 1

19

如果要在 SQL Server 中运行此查询,则可以使用以下PIVOT函数:

select *
from
(
  select role, familyname, givenname, skill,
    level, id, date, 'Y' flag
  from yourtable
) src
pivot
(
  max(flag)
  for date in ([2013-03-27], [2013-03-26],
               [2013-03-25], [2013-03-24])
) piv

请参阅带有演示的 SQL Fiddle

或者您可以使用聚合函数和CASE语句:

select role, familyname, givenname, skill,
  level, id,
  max(case when date = '2013-03-27' then flag end) '2013-03-27',
  max(case when date = '2013-03-26' then flag end) '2013-03-26',
  max(case when date = '2013-03-25' then flag end) '2013-03-25',
  max(case when date = '2013-03-24' then flag end) '2013-03-24'
from
(
  select role, familyname, givenname, skill,
    level, id, date, 'Y' flag
  from yourtable
) src
group by role, familyname, givenname, skill,
  level, id

请参阅带有演示的 SQL Fiddle

两者都给出结果:

|              ROLE | FAMILYNAME | GIVENNAME | SKILL |           LEVEL |  ID | 2013-03-27 | 2013-03-26 | 2013-03-25 | 2013-03-24 |
----------------------------------------------------------------------------------------------------------------------------------
| Vision Supervisor |   Unsworth |     Simon |    10 | Telegenic Staff | 664 |          Y |          Y |          Y |          Y |

如果您知道要转置的值,则上述方法非常有用,但如果您不知道,则可以使用类似于以下的动态 sql:

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

select @cols = STUFF((SELECT ',' + QUOTENAME(convert(char(10), date, 120)) 
                    from yourtable
                    group by date
                    order by date desc
            FOR XML PATH(''), TYPE
            ).value('.', 'NVARCHAR(MAX)') 
        ,1,1,'')

set @query = 'SELECT role, familyname, givenname, skill,
                    level, id,' + @cols + ' from 
             (
                select role, familyname, givenname, skill,
                    level, id, date, ''Y'' flag
                from yourtable
            ) x
            pivot 
            (
                max(flag)
                for date in (' + @cols + ')
            ) p '

execute(@query)

请参阅带有演示的 SQL Fiddle

于 2012-12-20T17:45:13.190 回答