sql - 对分层数据进行 PIVOT

标签 sql sql-server pivot

我有一个这样的表:

Id  Name                parentId
--------------------------------
5   Rollers             2
2   Paint and Brushes   1
1   Decorating          NULL

使用 PIVOT 或任何其他单个查询,我可以获得如下输出:

cat1id   cat1name      cat2id    cat2name             cat3id    cat3Name
------------------------------------------------------------------------
1        Decorating    2         Paint and Brushes    5         Rollers

最佳答案

您可以使用 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 Demo

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

;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 with Demo

两者的结果相同:

| 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 |

关于sql - 对分层数据进行 PIVOT,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11227924/

相关文章:

Java 数据库最小化连接创建

c# - 如何使用 SMO 获取和设置 SQL Server 2008 表的描述属性?

c# - 使用多个 DB 架构的 EF Core 迁移

python - 如何在 Pandas 中使用groupby根据另一列中的标准计算百分比/比例总数

java - 使用 TRANSFORM 和 PIVOT 的交叉表查询重复行

mysql - 如何删除数据透视表中的空值?

sql - Mysql COUNT(*) 在多个表上

sql - 连接 2 个不明确的 SQL 表

mysql行编号重置每个不同的记录值

sql - xlock、rowlock、holdlock 是否正确?