【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 );

 

转载于:https://www.cnblogs.com/handongxue/p/10677569.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值