如何旋转表格?

发布于 2024-10-15 07:58:13 字数 761 浏览 8 评论 0原文

我的表有这样的结构

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

p=present   
a=absent   

这个表将被更改为

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

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

晒暮凉 2024-10-22 07:58:13

以下是使用 unpivotpivot 执行此操作的方法:

静态查询:

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

您也可以使用 动态 SQL 但您需要先获取列列表,然后才能unpivot pivot

动态查询:

-- 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)

结果:

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

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
深海蓝天 2024-10-22 07:58:13

这是可以做到的,称为表转置。在 SQL Server 上,它需要构建动态 SQL 语句并使用 EXEC 语句执行它。

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.

森罗 2024-10-22 07:58:13

您必须旋转子代码列,然后取消旋转其他滚动列。

透视表将是 unpivot 语句内的派生表。

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

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
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文