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

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

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

我的桌子是这样的:

我想要这个:

【问题讨论】:

  • 能否提供表格的文字说明?

标签: oracle plsql pivot oracle-sqldeveloper


【解决方案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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-09-17
    • 2013-10-24
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多