SQL Pivot 日期列? [英] SQL Pivot on dates column?

查看:29
本文介绍了SQL Pivot 日期列?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I'm fairly new to SQL but believe me I have searched for help before posting this.

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

I have a query which returns a list of people assigned to jobs, also the jobs have varying length and people who are assigned to those jobs are working different lengths.

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

What I am trying to do is convert what is a list of similar records with the only variable changing is the date, and some how pivot this data so that the dates become column headings and the rows represent a BOOL yes/no.

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

This is the data I'm getting back currently. JSON encoded

{"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":"是"}]}

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

I'm sure this is some kind of PIVOT query but I cant get it to work.

谢谢

推荐答案

如果你打算在 SQL Server 中运行这个查询,那么你可以使用 PIVOT 函数:

If you are going to be running this query in SQL Server, then you can use the PIVOT function:

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 语句:

Or you can use an aggregate function and a CASE statement:

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:

The above works great if you know the values to transpose, but you if you don't then you can use dynamic sql similar to this:

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

这篇关于SQL Pivot 日期列?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆