Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/sql/80.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/sql-server/25.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Sql 以分层数据为中心_Sql_Sql Server_Pivot - Fatal编程技术网

Sql 以分层数据为中心

Sql 以分层数据为中心,sql,sql-server,pivot,Sql,Sql Server,Pivot,我有一张这样的桌子: Id Name parentId -------------------------------- 5 Rollers 2 2 Paint and Brushes 1 1 Decorating NULL cat1id cat1name cat2id cat2name cat3id cat3Name -------------------

我有一张这样的桌子:

Id Name parentId -------------------------------- 5 Rollers 2 2 Paint and Brushes 1 1 Decorating NULL cat1id cat1name cat2id cat2name cat3id cat3Name ------------------------------------------------------------------------ 1 Decorating 2 Paint and Brushes 5 Rollers Id名称parentId -------------------------------- 5个滚轮2 2油漆和刷子1 1装饰空 使用PIVOT或任何其他单个查询,可以获得如下输出:

Id Name parentId -------------------------------- 5 Rollers 2 2 Paint and Brushes 1 1 Decorating NULL cat1id cat1name cat2id cat2name cat3id cat3Name ------------------------------------------------------------------------ 1 Decorating 2 Paint and Brushes 5 Rollers cat1id CAT1名称cat2id CAT2名称cat3id CAT3名称 ------------------------------------------------------------------------ 1个装饰2个油漆和刷子5个滚筒
您可以使用
PIVOT
UNPIVOT
和递归查询来执行此操作

静态版本,用于将值硬编码到转换后的:

;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

动态版本,在运行时生成值:

;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

这两种方法的结果相同:

| 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 |
看这个问题,和你的类似。