How to Pivot a Table?

时光怂恿深爱的人放手 提交于 2019-12-21 22:52:07

问题


My table has this structure

subcode date        rol1   rol2 rol3 rol4 rol5 rol6 upto rol60
--------------------------------------------------------------
mc1603  12/03/2011  p      p    a    p    p    p         a
mc1604  12/03/2011  p      p    a    p    p    p         a            
mc1605  12/03/2011  p      p    a    p    p    p         a
mc1606  12/03/2011  p      p    a    p    p    p         a

here

p=present   
a=absent   

this table will be change into

rollno   mc1603 mc1604  mc1605 mc1606 date 
-------------------------------------------------
rol1     p      p       p      p      12/03/2011
rol2     p      p       p      p      12/03/2011
rol3     p      p       a      p      12/03/2011

回答1:


Here is how you would do this with both an unpivot and then pivot:

Static query:

create table #temp
(
    subcode varchar(50),
    date    smalldatetime,
    rol1    varchar(1),
    rol2    varchar(1),
    rol3    varchar(1),
    rol4    varchar(1),
    rol5    varchar(1),
    rol6    varchar(1),
    rol60   varchar(1)
)

insert into #temp values('mc1603', '03/12/2011', 'p', 'p', 'a', 'p', 'p', 'p', 'a')
insert into #temp values('mc1604', '03/12/2011', 'p', 'p', 'a', 'p', 'p', 'p', 'a')
insert into #temp values('mc1605', '03/12/2011', 'p', 'p', 'a', 'p', 'p', 'p', 'a')
insert into #temp values('mc1606', '03/12/2011', 'p', 'p', 'a', 'p', 'p', 'p', 'a')

SELECT p.rollNo, p.[mc1603], p.[mc1604], p.[mc1605], p.[mc1606], p.date
FROM
(
    SELECT *
    FROM
    (
        select subcode, date, rol1, rol2, rol3, rol4, rol5, rol6, rol60
        from #temp
    ) x
    UNPIVOT
    (
        presence FOR rollNo IN([rol1], [rol2], [rol3], [rol4], [rol5], [rol6], [rol60])
    ) unpiv
) x
PIVOT
(
    max(presence)
    FOR subcode IN([mc1603], [mc1604], [mc1605], [mc1606])
) p

drop table #temp

You can also do this with Dynamic SQL but you will need to get the list of columns first before you can unpivot and then pivot:

Dynamic query:

-- get your roll no columns
DECLARE @cols AS VARCHAR(MAX),
    @query  AS VARCHAR(MAX);

SET @cols = STUFF((SELECT ',' + QUOTENAME(c.Name) 
            FROM sys.columns c
            WHERE OBJECT_ID = OBJECT_ID('dbo.temp') AND c.NAME LIKE 'rol%'
            FOR XML PATH(''), TYPE
            ).value('.', 'VARCHAR(MAX)') 
        ,1,1,'')

-- get your dynamic string
set @query = 'SELECT p.rollNo, p.[mc1603], p.[mc1604], p.[mc1605], p.[mc1606], p.date
        FROM
        (
            SELECT *
            FROM
            (
                select subcode, date, ' + @cols + ' 
                from temp
            ) x
            UNPIVOT
            (
                presence FOR rollNo IN(' + @cols + ')
            ) unpiv 
        ) x
        PIVOT
        (
            max(presence)
            FOR subcode IN([mc1603], [mc1604], [mc1605], [mc1606])
        ) p'

exec(@query)

Results:

rol1    p   p   p   p   2011-03-12 00:00:00
rol2    p   p   p   p   2011-03-12 00:00:00
rol3    a   a   a   a   2011-03-12 00:00:00
rol4    p   p   p   p   2011-03-12 00:00:00
rol5    p   p   p   p   2011-03-12 00:00:00
rol6    p   p   p   p   2011-03-12 00:00:00
rol60   a   a   a   a   2011-03-12 00:00:00



回答2:


This can be done, it's called a table transpose. On SQL Server it requires building a dynamic SQL statement and executing it using the EXEC statement.




回答3:


You have to pivot the subcode column, and then unpivot the other roll columns.

The pivoted table would be a derived table inside the unpivot statement.

select * from
( 
SELECT * FROM () Table0 PIVOT () Table1
) Table2
UNPIVOT () Table3


来源:https://stackoverflow.com/questions/4857752/how-to-pivot-a-table

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!