透视动态列[重复]

Posted

技术标签:

【中文标题】透视动态列[重复]【英文标题】:Pivoting dynamic columns [duplicate] 【发布时间】:2020-08-24 19:47:23 【问题描述】:

我试图根据行动态创建列。

我正在使用子句 PIVOT XML,但我没有将 XML 列解析为列。

我的桌子是这样的:

我想要这个:

【问题讨论】:

能否提供表格的文字说明? 【参考方案1】:

Pivot xml 返回 XMLType,但您显示了标准表结构。 透视xml示例:

with t(id, dt, medical_test,positive) as (
select 1, date'2020-04-20','blood count'                       , 1 from dual union all
select 2, date'2020-04-26','bone marrow aspiration'            , 1 from dual union all
select 2, date'2020-04-30','cephalin-cholesterol flocculation' , 0 from dual union all
select 2, date'2020-05-05','hematocrit'                        , 0 from dual union all
select 3, date'2020-04-02','lumbar puncture'                   , 0 from dual union all
select 3, date'2020-04-13','Pap smear'                         , 1 from dual union all
select 4, date'2020-06-06','cephalin-cholesterol flocculation' , 1 from dual
)
select *
from t
pivot xml(
   max(positive) as positive
   for medical_test in (any)
);

结果:

        ID DT                  MEDICAL_TEST_XML
---------- ------------------- ------------------------------------------------------------------------------------
         1 2020-04-20 00:00:00 <PivotSet><item><column name = "MEDICAL_TEST">blood count</column><column name = "POSITIVE">1</column></item></PivotSet>
         2 2020-04-26 00:00:00 <PivotSet><item><column name = "MEDICAL_TEST">bone marrow aspiration</column><column name = "POSITIVE">1</column></item></PivotSet>
         2 2020-04-30 00:00:00 <PivotSet><item><column name = "MEDICAL_TEST">cephalin-cholesterol flocculation</column><column name = "POSITIVE">0</column></item></PivotSet>
         2 2020-05-05 00:00:00 <PivotSet><item><column name = "MEDICAL_TEST">hematocrit</column><column name = "POSITIVE">0</column></item></PivotSet>
         3 2020-04-02 00:00:00 <PivotSet><item><column name = "MEDICAL_TEST">lumbar puncture</column><column name = "POSITIVE">0</column></item></PivotSet>
         3 2020-04-13 00:00:00 <PivotSet><item><column name = "MEDICAL_TEST">Pap smear</column><column name = "POSITIVE">1</column></item></PivotSet>
         4 2020-06-06 00:00:00 <PivotSet><item><column name = "MEDICAL_TEST">cephalin-cholesterol flocculation</column><column name = "POSITIVE">1</column></item></PivotSet>

使用简单的 PIVOT 更容易实现您想要的:

with t(id, dt, medical_test,positive) as (
select 1, date'2020-04-20','blood count'                       , 1 from dual union all
select 2, date'2020-04-26','bone marrow aspiration'            , 1 from dual union all
select 2, date'2020-04-30','cephalin-cholesterol flocculation' , 0 from dual union all
select 2, date'2020-05-05','hematocrit'                        , 0 from dual union all
select 3, date'2020-04-02','lumbar puncture'                   , 0 from dual union all
select 3, date'2020-04-13','Pap smear'                         , 1 from dual union all
select 4, date'2020-06-06','cephalin-cholesterol flocculation' , 1 from dual
)
select--+ no_merge
  piv.*
from t
pivot (
   max(positive)
   for medical_test in (
      'blood count'                       ,
      'bone marrow aspiration'            ,
      'cephalin-cholesterol flocculation' ,
      'hematocrit'                        ,
      'lumbar puncture'                   ,
      'Pap smear'                         
      )
) piv
order by 1,2;

结果:

        ID DT                  'blood count' 'bone marrow aspiration' 'cephalin-cholesterol flocculation' 'hematocrit' 'lumbar puncture' 'Pap smear'
---------- ------------------- ------------- ------------------------ ----------------------------------- ------------ ----------------- -----------
         1 2020-04-20 00:00:00             1
         2 2020-04-26 00:00:00                                      1
         2 2020-04-30 00:00:00                                                                          0
         2 2020-05-05 00:00:00                                                                                       0
         3 2020-04-02 00:00:00                                                                                                         0
         3 2020-04-13 00:00:00                                                                                                                     1
         4 2020-06-06 00:00:00                                                                          1

【讨论】:

以上是关于透视动态列[重复]的主要内容,如果未能解决你的问题,请参考以下文章

SQL 动态数据透视表列顺序

基于拆分值透视动态列

在 Oracle 中使用动态列进行透视

使用数据透视的动态列

如何在动态数据透视中先按行排序,然后按列排序

具有动态列的 MySQL 数据透视表查询