2014-02-19 89 views
0

我試圖建立一個與n列,每列表示一個層次結構的一部分,頂級父母在左邊,後裔在右邊。每列下應僅列出該列/層次結構的值。該圖顯示了我迄今爲止的情況,這是不正確的,因爲(1)NULLS顯示在間隙中,(2)我的「sometext」沒有顯示。帶有值的SQL Server 2012 Pivot Dynamic?

什麼是正確的方式來獲得我需要的結果?

謝謝。

當前代碼:

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) 

這是所產生的集。

Set with NULLs in the wrong places

這是我想要的(藉口外來引號)設定。

enter image description here

所有的想法表示讚賞。

謝謝。

回答

2

您在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) | 
+0

令人驚歎。我想我明白了。由於我花了一個小時+得到了這麼多,我可以花一個小時瞭解數據透視中序列的需求。謝謝! – Snowy

+1

@Snowy由於數據透視表是聚合數據,如果'hiercode'中的每行沒有某種唯一值,那麼max將只返回一行。通過創建該號碼,您將返回所需的每一行。當爲聚合函數完成分組時,將包括子查詢中的任何列。 :) – Taryn