首页 > 数据库 >Oracle中行转列(pivot)函数解析(二)

Oracle中行转列(pivot)函数解析(二)

时间:2022-09-04 19:34:56浏览次数:93  
标签:fee xmdm sum xmje decode 转列 sl Oracle pivot

Oracle行转列就是把某一个字段的值作为唯一值,然后另外一个字段的行值转换成它的列值。

案例原始数据如下:

 

 方法一:利用group by实现

select t.mr_sl_id,
       sum(decode(xmdm, '01', t.xmje, 0)) 床位费,
       sum(decode(xmdm, '02', t.xmje, 0)) 诊察费,
       sum(decode(xmdm, '03', t.xmje, 0)) 检查费,
       sum(decode(xmdm, '04', t.xmje, 0)) 化验费,
       sum(decode(xmdm, '05', t.xmje, 0)) 治疗费,
       sum(decode(xmdm, '06', t.xmje, 0)) 手术费,
       sum(decode(xmdm, '07', t.xmje, 0)) 护理费,
       sum(decode(xmdm, '08', t.xmje, 0)) 卫生材料费,
       sum(decode(xmdm, '09', t.xmje, 0)) 西药费,
       sum(decode(xmdm, '10', t.xmje, 0)) 中药饮片费,
       sum(decode(xmdm, '11', t.xmje, 0)) 中成药费,
       sum(decode(xmdm, '12', t.xmje, 0)) 一般诊疗费,
       sum(decode(xmdm, '13', t.xmje, 0)) 挂号费,
       sum(decode(xmdm, '14', t.xmje, 0)) 其他费
  from hos_drgs.sl_charge t
 where t.mr_sl_id = 'ZY110000557222'
 group by t. mr_sl_id
View Code

 

 方法二:Oracle11g之后提供了自带函数PIVOT可以完美解决这个行转列的需求,具体语法结构如下:

select *
   from (数据查询集)
 pivot
 (
        sum(xmje /*行转列后 列的值*/
        for xmdm /*需要行转列的列*/
        in(转换后列的值))
 )
View Code
select *
  from (select mr_sl_id, xmdm, sum(xmje) fee
          from hos_drgs.sl_charge
         where mr_sl_id = 'ZY110000557222'
         group by mr_sl_id, xmdm)
pivot(sum(fee)
   for xmdm in('01' as bed_fee,
               '02' as consultation_fee,
               '03' as inspection_fee,
               '04' as assay_fee,
               '05' as treatment_fee,
               '06' as operation_fee,
               '07' as nursing_fee,
               '08' as sanitary_material_fee,
               '09' as western_medicine_fee,
               '10' as cn_medicine_pieces_fee,
               '11' as cn_patent_medicine_fee,
               '12' as general_medical_fee,
               '13' as regist_fee,
               '14' as other_fee))
View Code

 

 参考博客:https://blog.csdn.net/langweixiana/article/details/116011508

 

标签:fee,xmdm,sum,xmje,decode,转列,sl,Oracle,pivot
From: https://www.cnblogs.com/daytoy105/p/16655746.html

相关文章

  • Oracle中ESCAPE关键字用法
    select*fromhdrg.qcs_dict_item_ruleswhererule_codelike'rule/_m%'escape'/';注释:此处like后面的_下划线并不是占位符的意思,而是他原本下划线的意思。补......
  • Oracle中行转列函数(一)
    1、wm_concat(列名)解析:该函数可以把列值以“,”号分割起来,并显示成一行。例:selectwm_concat(item_code)fromhdrg.qcs_dict_item_detailwheretable_name='d......
  • oracle中merge into用法解析
    1mergeinto的形式mergeinto[target-table]ausing[source-tablesql]bon([conditionalexpression]and[...]...)whenmatchedthen[updat......
  • Oracle数据库导入、导出dmp文件
    1、数据库导出dmp文件exphdrg/hdrg@10.1.60.21:1521/orclfile=D:\hdrg.dmp说明:exp用户名/密码@数据库名file=E:\file.dmptables=(要导出的表名称,以逗号隔开)2、o......
  • Navicat连接Oracle时报错 “ORA-28547:连接服务器失败,可能是oracle net 管理错误“ 或
    Navicat连接Oracle时报错“ORA-28547:连接服务器失败,可能是oraclenet管理错误“ 或者“ORA-03135:ConnectionLostContact”使用Navicat连接oracle数据库时报ORA-2......
  • 【Oracle初学者】ORA-01034: ORACLE not available
    系统报错代码ORA-01034:ORACLEnotavailable出现原因//在启动实例时,关闭了数据库,导致外部软件无法访问Oracle数据库(大部分都是因为数据库监听或者服务关闭导致出现的......
  • Oracle数据库
    OracleWin+Rsqlplus/nolog确定connsystem/orcl已连接createuserscottidentifiedbytiger;用户已创建grantconnect,resourcetoscott;授权成功connsco......
  • oracle 分组 聚合数据 单字段聚合问题
    SELECT--max加这个只是为了让字段不在分组里写 max(id)id, max(stockorg_name)stockorg_name, max(vbillcode)vbillcode, max(source_no)source_no,--newvbil......
  • 查看Oracle当前用户下的(表,视图,同义词,索引等...)
     copy自:查看Oracle当前用户下的(表,视图,同义词,索引等...)表空间–查看当前用户表空间selectusername,default_tablespacefromuser_users;selectdefault_tablespace......
  • oracle创建序列
    1创建一个序列,这个是给userLoginlog表用的,所以起了个关联名createsequenceseq_on_userloginincrementby1startwith1nomaxvaluenocyclenocache;2插入数据......