4

私はこのようなテーブルを持っています:

ID名parentId
--------------------------------
5ローラー2
2ペイントとブラシ1
1NULLを飾る

PIVOTまたはその他の単一のクエリを使用して、次のような出力を取得できますか?

cat1id cat1name cat2id cat2name cat3id cat3Name
-------------------------------------------------- ----------------------
1飾る2ペンキとブラシ5ローラー
4

1 に答える 1

7

と再帰クエリを使用してこれを実行でき ますPIVOTUNPIVOT

静的バージョンは、変換された値をハードコードする場所です。

;with hd (id, name, parentid, category)
as
(
  select id, name, parentid, 1 as category
  from yourtable
  where parentid is null
  union all
  select t1.id, t1.name, t1.parentid, hd.category +1
  from yourtable t1
  inner join hd
    on t1.parentid = hd.id
),
unpiv as
(
  select value, 'cat_'+cast(category as varchar(5))+'_'+ col col_name
  from
  (
    select cast(id as varchar(17)) id, name, parentid, category
    from hd
  ) src
  unpivot
  (
    value for col in (id, name)
  ) un
)
select [cat_1_id], [cat_1_name],
                   [cat_2_id], [cat_2_name],
                   [cat_3_id], [cat_3_name]
from unpiv
pivot
(
  max(value)
  for col_name in ([cat_1_id], [cat_1_name],
                   [cat_2_id], [cat_2_name],
                   [cat_3_id], [cat_3_name])
) piv

デモで SQL Fiddle を参照してください

動的バージョン。値は実行時に生成されます。

;with hd (id, name, parentid, category)
as
(
  select id, name, parentid, 1 as category
  from yourtable
  where parentid is null
  union all
  select t1.id, t1.name, t1.parentid, hd.category +1
  from yourtable t1
  inner join hd
    on t1.parentid = hd.id
)
select category categoryNumber
into #temp
from hd

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

select @cols = STUFF((SELECT distinct ',' + quotename('cat_'+cast(CATEGORYNUMBER as varchar(10))+'_'+col) 
                  from #temp
                  cross apply (select 'id' col
                               union all 
                               select 'name' col) src
            FOR XML PATH(''), TYPE
            ).value('.', 'NVARCHAR(MAX)') 
        ,1,1,'')

set @query = ';with hd (id, name, parentid, category)
              as
              (
                select id, name, parentid, 1 as category
                from yourtable
                where parentid is null
                union all
                select t1.id, t1.name, t1.parentid, hd.category +1
                from yourtable t1
                inner join hd
                  on t1.parentid = hd.id
              ),
              unpiv as
              (
                select value, ''cat_''+cast(category as varchar(5))+''_''+ col col_name
                from
                (
                  select cast(id as varchar(17)) id, name, parentid, category                 
                  from hd
                ) src
                unpivot
                (
                  value for col in (id, name)
                ) un
              )
              select '+@cols+'
              from unpiv
              pivot
              (
                max(value)
                for col_name in ('+@cols+')
               ) piv'

execute(@query)

drop table #temp

デモで SQL Fiddle を参照してください

結果は両方とも同じです。

| CAT_1_ID | CAT_1_NAME | CAT_2_ID |        CAT_2_NAME | CAT_3_ID | CAT_3_NAME |
--------------------------------------------------------------------------------
|        1 | Decorating |        2 | Paint and Brushes |        5 |    Rollers |
于 2012-11-09T14:22:46.353 に答える