【Oracle】Oracle中多列转成多行

  今天做BI的时候遇到这样一个问题,要将多列转化成多行,简单记录下是如何操作的。

1、问题:要将第一个图的两条数据转化成第二个图显示的结果

                                                               

2、实现:具体实现要使用到Oracle的 unpivot 函数

 1 select in_hos_num,patient_name,out_hos_date,l_hos_type,
 2     icd_9_code, icd_9_name,in_hos --取得是别名
 3 from dim_diagnosis   
 4     unpivot((icd_9_code,icd_9_name,in_hos)--设置多列合并成一列的别名
 5     for qc in ((icd_9_main,icd_9_main_name,in_hos_main) ,--实际的字段和别名对应
 6         (icd_9_other1,icd_9_other1_name, in_hos_other1),
 7         (icd_9_other2,icd_9_other2_name, in_hos_other2),
 8         (icd_9_other3,icd_9_other3_name, in_hos_other3),
 9         (icd_9_other4,icd_9_other4_name, in_hos_other4),
10         (icd_9_other5,icd_9_other5_name, in_hos_other5),
11         (icd_9_other6,icd_9_other6_name, in_hos_other6),
12         (icd_9_other7,icd_9_other7_name, in_hos_other7),
13         (icd_9_other8,icd_9_other8_name, in_hos_other8),
14         (icd_9_other9,icd_9_other9_name, in_hos_other9),
15         (icd_9_other10,icd_9_other10_name, in_hos_other10),
16         (icd_9_other11,icd_9_other11_name, in_hos_other11),
17         (icd_9_other12,icd_9_other12_name, in_hos_other12),
18         (icd_9_other13,icd_9_other13_name, in_hos_other13))  
19 );

 

posted @ 2019-04-09 16:06  韩小韩Sunny  阅读(1814)  评论(0编辑  收藏  举报