使用GroupBy获取PIVOT

时间:2013-09-19 19:07:26

标签: sql sql-server sql-server-2008 pivot pivot-table

我有这张表:

Person      Job     Day  EndDay
1           101     1    12
1           102     2    11
1           103     3    11
3           101     1    11
3           102     2    11 
3           JobOff  3    11
2           101     1    11
2           102     2    11
2           103     3    11
2           JobOff  4    11
...
PS:我的日子是'N',我不知道。因此,静态SQL在这里是不可行的。

我需要将其更改为此格式:

Day (Columns)
Person  (Rows)
Person  1    2    3       4
1       101  102  103     N/A
2       101  102  103     JobOff
3       101  102  JobOff  N/A

任何帮助将不胜感激。我是SQL的新手。

感谢Roman,我有这个S.Procedure:

CREATE PROCEDURE [dbo].[MyStoredProcedure] AS
BEGIN

declare @stmt nvarchar(max)

select @stmt =
    isnull(@stmt + ', ', '') + 
    'isnull(max(case when [Day] = ' + cast([Day_I] as nvarchar(max)) + 
    ' then Job end), ''N/A'') as [' + cast([Day_I] as nvarchar(max)) + ']'
from (select distinct [Day_I] from dbo.Solution) as t
order by [Day_I]

select @stmt = '
select
    Person_I, ' + @stmt +'
from dbo.Solution_Format
group by Person_I'

END

但是当我执行这样的存储过程时:

exec MyStoredProcedure

它显示我没有结果。没错!

2 个答案:

答案 0 :(得分:3)

select
    Person,
    isnull(max(case when [Day] = 1 then Job end), 'N/A') as [1],
    isnull(max(case when [Day] = 2 then Job end), 'N/A') as [2],
    isnull(max(case when [Day] = 3 then Job end), 'N/A') as [3],
    isnull(max(case when [Day] = 4 then Job end), 'N/A') as [4]
from Table1
group by Person

<强> sql fiddle demo

如果您需要使用任意数量的列,也可以使用动态SQL执行此操作:

create procedure [dbo].[MyStoredProcedure]
as
begin   
    declare @stmt nvarchar(max)

    select @stmt =
        isnull(@stmt + ', ', '') + 
        'isnull(max(case when [Day] = ' + cast([Day] as nvarchar(max)) + 
        ' then Job end), ''N/A'') as [' + cast([Day] as nvarchar(max)) + ']'
    from (select distinct [Day] from Table1) as t
    order by [Day]

    select @stmt = '
    select
        Person, ' + @stmt +'
    from Table1
    group by Person'

    exec sp_executesql
        @stmt = @stmt
end

<强> sql fiddle demo

答案 1 :(得分:2)

执行此操作的一种方法是使用多个自联接

Select
  t1.Person,
  COALESCE(t1.Job, 'N/A') [1],
  COALESCE(t2.Job, 'N/A' )[2],
  COALESCE(t3.Job, 'N/A') [3], 
  COALESCE(t4.Job, 'N/A') [4]
FROM 
  table1 t1
  LEFT JOIN table1  t2
  ON t1.Person =  t2.Person
    and t2.Day = 2
  LEFT JOIN table1  t3
   ON t1.Person =  t3.Person
     and t3.Day = 3
  LEFT JOIN table1  t4
    ON t1.Person =  t4.Person
     and t4.Day = 4
WHERE
   t1.Day = 1
ORDER BY 
  t1.Person

DEMO

另一种方法是使用MAX(CASE但是我不喜欢使用像这样的聚合。

Select
  t1.Person,
  COALESCE(MAX(CASE WHEN t1.Day = 1 then t1.Job END), 'N/A') [1],
  COALESCE(MAX(CASE WHEN t1.Day = 2 then t1.Job END), 'N/A') [2],
  COALESCE(MAX(CASE WHEN t1.Day = 3 then t1.Job END), 'N/A') [3],
  COALESCE(MAX(CASE WHEN t1.Day = 4 then t1.Job END), 'N/A') [4]

FROM 
  table1 t1
GROUP BY
  t1.Person

DEMO

最后有一个我不太喜欢的枢轴条款,因为MAX在这里

SELECT person, 
       COALESCE([1], 'N/A') [1], 
       COALESCE([2], 'N/A') [2], 
       COALESCE([3], 'N/A') [3], 
       COALESCE([4], 'N/A') [4] 
FROM   (SELECT t1.person, 
               t1.day, 
               t1.job 
        FROM   table1 t1) p 
       PIVOT (Max (job) 
             FOR day IN ( [1], 
                          [2], 
                          [3], 
                          [4]) ) AS pvt 

DEMO