【问题标题】:SQL Server 2012 Pivot Dynamic with Values?SQL Server 2012 Pivot Dynamic 与值?
【发布时间】:2014-03-20 01:43:00
【问题描述】:

我正在尝试使用 n 列构建一个集合,每一列代表层次结构的一部分,左侧是顶级父级,右侧是后代。在每列下应仅列出该列/层次结构的值。该图像显示了我到目前为止所拥有的,这是不正确的,因为 (1) NULLS 显示在间隙中,并且 (2) 我的“sometext”没有显示。

获得所需结果的正确方法是什么?

谢谢。

当前代码:

set nocount on
IF OBJECT_ID('tempdb..#tst') IS NOT NULL DROP TABLE #tst
create table #tst ( hiercode varchar(10) , sometext varchar(80) )
Insert into #tst
Select '/7/' , 'aaaa' 
Union All
Select '/7/' , '1' 
Union All
Select '/7/' , '2' 
Union All
Select '/7/1/' , 'bbbb' 
Union All
Select '/7/1/' , '3' 
Union All
Select '/7/1/' , '4' 
Union All
Select '/7/1/' , '5' 
Union All
Select '/7/1/1/' , 'cccc' 
Union All
Select '/7/1/1/' , '6' 
Union All
Select '/7/1/1/' , '7' 
Union All
Select '/7/1/1/' , '8' 
Union All
Select '/7/1/2/' , 'dddd' 
Union All
Select '/7/1/3/' , 'eeee' 
Union All
Select '/7/2/' , 'ffff' 
Union All
Select '/7/2/1/' , 'gggg' 
Union All
Select '/7/2/1/' , '9' 
Union All
Select '/7/2/1/' , '10' 
Union All
Select '/7/2/1/' , '11' 
Union All
Select '/7/2/2/' , 'hhhh' 
Union All
Select '/7/2/3/' , 'iiii' 
Union All
Select '/7/2/4/'  , 'jjjj' 
Union All
Select '/7/3/' , 'kkkk'
Union All
Select '/7/3/1/' , 'llll' 
Union All
Select '/7/3/2/'  , 'mmmm' 
Union All
Select '/7/3/3/' , 'nnnn' 
declare @cols as nvarchar(max) = 
    STUFF((SELECT distinct ',' + QUOTENAME(c.hiercode) 
            FROM #tst c
            FOR XML PATH(''), TYPE
            ).value('.', 'NVARCHAR(MAX)') 
        ,1,1,'') 
select @cols
declare @statemt as nvarchar(max) = 
     'SELECT  ' + @cols + ' from 
            (
                select hiercode
                    , sometext
                from #tst
           ) x
            pivot 
            (
                 max(hiercode)
                for hiercode in (' + @cols + ')
            ) p '
select @statemt
execute(@statemt)

这是生成的集合。

这是我想要的集合(请原谅多余的引号)。

感谢所有想法。

谢谢。

【问题讨论】:

    标签: sql-server dynamic pivot


    【解决方案1】:

    您在 PIVOT 的聚合部分中使用与新列名称相同的列。

    max() 聚合应该选择您希望在列中显示的数据。

    尝试使用:

    declare @cols as nvarchar(max) = 
        STUFF((SELECT distinct ',' + QUOTENAME(c.hiercode) 
                FROM #tst c
                FOR XML PATH(''), TYPE
                ).value('.', 'NVARCHAR(MAX)') 
            ,1,1,'') 
    
    select @cols
    declare @statemt as nvarchar(max) = 
         'SELECT  ' + @cols + ' from 
                (
                    select hiercode
                        , sometext
                    from #tst
               ) x
                pivot 
                (
                     max(sometext)
                    for hiercode in (' + @cols + ')
                ) p '
    select @statemt
    execute(@statemt);
    

    Demo。上述版本将为每个hiercode 返回一行,如果您想为每个hiercode 返回多行,那么您需要创建一个用于分组的唯一序列 - 我会使用row_number():

    declare @cols as nvarchar(max) = 
        STUFF((SELECT distinct ',' + QUOTENAME(c.hiercode) 
                FROM #tst c
                FOR XML PATH(''), TYPE
                ).value('.', 'NVARCHAR(MAX)') 
            ,1,1,'') 
    
    declare @statemt as nvarchar(max) = 
         'SELECT  ' + @cols + ' from 
                (
                    select hiercode
                        , sometext,
                        row_number() over(partition by hiercode
                                          order by hiercode) seq
                    from #tst
               ) x
                pivot 
                (
                     max(sometext)
                    for hiercode in (' + @cols + ')
                ) p '
    execute(@statemt);
    

    SQL Fiddle with Demo。这将生成:

    |    /7/ | /7/1/ | /7/1/1/ | /7/1/2/ | /7/1/3/ |  /7/2/ | /7/2/1/ | /7/2/2/ | /7/2/3/ | /7/2/4/ |  /7/3/ | /7/3/1/ | /7/3/2/ | /7/3/3/ |
    |--------|-------|---------|---------|---------|--------|---------|---------|---------|---------|--------|---------|---------|---------|
    |   aaaa |  bbbb |    cccc |    dddd |    eeee |   ffff |    gggg |    hhhh |    iiii |    jjjj |   kkkk |    llll |    mmmm |    nnnn |
    |      1 |     3 |       6 |  (null) |  (null) | (null) |       9 |  (null) |  (null) |  (null) | (null) |  (null) |  (null) |  (null) |
    |      2 |     4 |       7 |  (null) |  (null) | (null) |      10 |  (null) |  (null) |  (null) | (null) |  (null) |  (null) |  (null) |
    | (null) |     5 |       8 |  (null) |  (null) | (null) |      11 |  (null) |  (null) |  (null) | (null) |  (null) |  (null) |  (null) |
    

    【讨论】:

    • 太棒了。我想我明白了。因为我花了一个多小时才走到这一步,所以我可以再花一个小时来了解枢轴中序列的需求。谢谢!
    • @Snowy 由于数据透视是聚合数据,如果hiercode 中的每一行没有某种唯一值,那么最大值将只返回一行。通过创建该数字,您将返回所需的每一行。为聚合函数完成分组时,将包含子查询中的任何列。 :)
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2013-03-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-11-17
    • 2014-12-30
    • 1970-01-01
    相关资源
    最近更新 更多