SQL复杂动态透视2

SQL complex dynamic Pivoting 2(SQL复杂动态透视2)
本文介绍了SQL复杂动态透视2的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好,我正在 SQL Server 中尝试对下表进行透视

Hi I am trying in SQL Server the pivoting for the following table

REFID | COL1 | COL2 | Sequence
1       abc    cde     1
1       lmn    rst     2
1       kna    asg     3
2       als    zkd     2
2       zpk    lad     1

我希望输出为

        REFID | 1COL1 | 2COL1 | 3COL1 |1COL2|2COL2|3COL2
           1    abc     lmn      kna    cde   rst   asg
           2    zpk     als      null   lad   zkd   null

原始表中的列数已知,但行数未知.谁能帮忙

The number of columns in the original table are known but the number of rows are not known. Can any one help

推荐答案

如果您想将 sequence 编号作为列名的一部分,那么您仍然需要取消转置您的 col1col2 列首先,然后应用枢轴.不同之处在于您会将 sequence 编号连接到在反透视过程中创建的列名.

If you want to include the sequence number as part of your column names, then you will still need to unpivot your col1 and col2 columns first, then apply the pivot. The difference is that you will concatenate the sequence number to your column names created during the unpivot process.

对于已知数量的值,查询将是:

For a known number of values the query would be:

select REFID, 
    [1col1], [2col1], [3col1],
    [1col2], [2col2], [3col2]
from 
(
    select REFID, 
        col = cast(Sequence as varchar(10))+ col, value
    from yourtable
    cross apply
    (
        select 'COL1', col1 union all
        select 'COL2', col2
    ) c (col, value)
) d
pivot
(
    max(value)
    for col in ([1col1], [2col1], [3col1],
                [1col2], [2col2], [3col2])
) piv
order by refid;

如果你有一个未知的数字,动态 SQL 版本将是:

Then if you have an unknown number the dynamic SQL version will be:

DECLARE @cols AS NVARCHAR(MAX),
    @query  AS NVARCHAR(MAX)

select @cols = STUFF((SELECT ',' + QUOTENAME(cast(Sequence as varchar(10))+ col) 
                    from yourtable
                    cross apply
                    (
                        select 'Col1', 1 union all
                        select 'Col2', 2
                    ) c(col, so)
                    group by Sequence, col, so
                    order by  so, sequence
                    FOR XML PATH(''), TYPE
            ).value('.', 'NVARCHAR(MAX)') 
        ,1,1,'')

set @query = 'SELECT refid, ' + @cols + ' 
            from 
            (
               select REFID, 
                    col = cast(Sequence as varchar(10))+ col, value
                from yourtable
                cross apply
                (
                    select ''COL1'', col1 union all
                    select ''COL2'', col2
                ) c (col, value)
            ) x
            pivot 
            (
                max(value)
                for col in (' + @cols + ')
            ) p 
            order by refid'

execute sp_executesql @query;

这篇关于SQL复杂动态透视2的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本站部分内容来源互联网,如果有图片或者内容侵犯您的权益请联系我们删除!

相关文档推荐

Execute complex raw SQL query in EF6(在EF6中执行复杂的原始SQL查询)
Hibernate reactive No Vert.x context active in aws rds(AWS RDS中的休眠反应性非Vert.x上下文处于活动状态)
Bulk insert with mysql2 and NodeJs throws 500(使用mysql2和NodeJS的大容量插入抛出500)
Flask + PyMySQL giving error no attribute #39;settimeout#39;(FlASK+PyMySQL给出错误,没有属性#39;setTimeout#39;)
auto_increment column for a group of rows?(一组行的AUTO_INCREMENT列?)
Sort by ID DESC(按ID代码排序)