SQL枢轴上的日期列? [英] SQL Pivot on dates column?

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

问题描述

我是相当新的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.

我所试图做的是转换是什么,唯一的变量变化类似记录列表是日期,以及一些如何透视这个数据,以便日期成为列标题和行重新present布尔是/否。

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恩codeD

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

{成果:[{角色:宣主管,familyname:昂斯沃斯,给定名称:西蒙,术:10,水平:上镜工作人员,ID:664,日期:2013年3月27日},{角色:宣主管,familyname:昂斯沃斯,给定名称 :西蒙,术:10,级别:上镜工作人员,ID:664,日期:2013年3月26日},{角色:宣主管,familyname:昂斯沃斯,给定名称:西蒙,术:10,级别:上镜工作人员,ID:664,日期:2013 -03-25},{角色:宣主管,familyname:昂斯沃斯,给定名称:西蒙,术:10,级别:上镜员工 ID:664,日期:2013年3月24日}]}

和我想得到的回复是:

{成果:[{角色:宣主管,familyname:昂斯沃斯,给定名称:西蒙,术:10,水平:上镜Staff\",\"id\":\"664\",\"2013-03-27\":\"YES\",\"2013-03-26\":\"YES\",\"2013-03-25\":\"YES\",\"2013-03-24\":\"YES\"}]}

我敢肯定,这是某种形式的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拨弄演示

或者你也可以使用一个聚合函数和一个 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拨弄演示

两者给出的结果:

|              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拨弄演示

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

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