SQL Pivot日期列?

时间:2012-12-20 17:16:06

标签: sql date boolean pivot

我对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查询,但我无法让它工作。

由于

1 个答案:

答案 0 :(得分:14)

如果要在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 with Demo

或者您可以使用聚合函数和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 with Demo

两者都给出结果:

|              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 with Demo