zoukankan      html  css  js  c++  java
  • oracle 行转列+列转行

    oracle实现行列转换的方法总结

      长->宽,方法一:group by - case when

    select
    		MDSENO,
    		max(case when CUTYPE = 0 then pjMECODE else null end) as cutype0,
    		max(case when CUTYPE = 1 then pjMECODE else null end) as cutype1,
    		max(case when CUTYPE = 2 then pjMECODE else null end) as cutype2,
    		max(case when CUTYPE = 3 then pjMECODE else null end) as cutype3,
    		max(case when CUTYPE = 4 then pjMECODE else null end) as cutype4,
    		max(case when CUTYPE = 5 then pjMECODE else null end) as cutype5,
    		max(case when CUTYPE = 6 then pjMECODE else null end) as cutype6,
    		max(case when CUTYPE = 7 then pjMECODE else null end) as cutype7,
    		max(case when CUTYPE = 8 then pjMECODE else null end) as cutype8,
    		max(case when CUTYPE = 9 then pjMECODE else null end) as cutype9,
    		max(case when CUTYPE = 10 then pjMECODE else null end) as cutype10,
    		max(case when CUTYPE = 11 then pjMECODE else null end) as cutype11,
    		max(case when CUTYPE = 12 then pjMECODE else null end) as cutype12,
    		max(case when CUTYPE = 13 then pjMECODE else null end) as cutype13,
    		max(case when CUTYPE = 14 then pjMECODE else null end) as cutype14,
    		max(case when CUTYPE = 15 then pjMECODE else null end) as cutype15,
    		max(case when CUTYPE = 16 then pjMECODE else null end) as cutype16,
    		max(case when CUTYPE = 17 then pjMECODE else null end) as cutype17,
    		max(case when CUTYPE = 18 then pjMECODE else null end) as cutype18,
    		max(case when CUTYPE = 19 then pjMECODE else null end) as cutype19,
    		max(case when CUTYPE = 20 then pjMECODE else null end) as cutype20,
    		max(case when CUTYPE = 21 then pjMECODE else null end) as cutype21,
    		max(case when CUTYPE = 22 then pjMECODE else null end) as cutype22,
    		max(case when CUTYPE = 23 then pjMECODE else null end) as cutype23,
    		max(case when CUTYPE = 24 then pjMECODE else null end) as cutype24,
    		max(case when CUTYPE = 25 then pjMECODE else null end) as cutype25,
    		--max(case when CUTYPE = 26 then pjMECODE else null end) as cutype26,
    		max(case when CUTYPE = 99 then pjMECODE else null end) as cutype99
    		
    	from
    	(
    		select
    			MDSENO,
    			CUTYPE,
    			LISTAGG(to_char(MECODE), ',') WITHIN GROUP(ORDER BY MECODE) AS pjMECODE
    			--LISTAGG(to_char(counts), ',') WITHIN GROUP(ORDER BY MECODE) AS pjcounts,
    			--LISTAGG(to_char(amounts), ',') WITHIN GROUP(ORDER BY MECODE) AS pjamounts
    		from
    		(
    			select
    				MDSENO, CUTYPE, MECODE  --, count(*) counts, sum(amount) as amounts
    			from
    				lu_yb_sbda_md_temp
    			group by
    				MDSENO, CUTYPE, MECODE
    			order by 
    				MDSENO, CUTYPE, MECODE
    		) t
    		group by
    			MDSENO, CUTYPE
    	) b
    	group by
    		MDSENO
    

      长->宽,方法二:pivot() within group

    select   *
    from (
         select
             MDSENO, CUTYPE, MECODE       --, count(*) counts, sum(amount) as amounts
         from
             lu_yb_sbda_md_temp
         group by
             MDSENO, CUTYPE, MECODE
    ) b 
    pivot(LISTAGG(to_char(MECODE), ',') WITHIN GROUP(ORDER BY MECODE)
        for CUTYPE in (0 as cutype0,1 as cutype1,2,3,4,5,6,7,8,9,10,11,12,13, 
            14,15,16,17,18,19,20,21,22,23,24,25,99));
    -- for CUTYPE in (select distinct cutype from lu_yb_sbda_md_temp)
    

      方法二实例:

    select * from 
    (select 1 as id, 'a' as x, 3 as v from dual
    union
    select 1 as id, 'b' as x, 4 as v from dual
    union
    select 2 as id, 'a' as x, 5 as v from dual
    union
    select 2 as id, 'c' as x, 6 as v from dual
    union
    select 2 as id, 'a' as x, 7 as v from dual) t
    pivot(sum(v) for x in ('a' as a, 'b', 'c'));
    

       存储过程实现动态行转列

    select *
    from (
    	select hicode, hiname, total, mecode_map as mecode, rate, avgfd
    	from lu_consu_mate_mename_0 b
    ) t
    pivot xml(max(rate) as rate, max(avgfd) as avgfd for mecode in (select distinct mecode_map from lu_consu_mate_mename_0))
    order by hicode
    

      

        

    参考资料:

    Oracle函数篇 - pivot行转列函数

    oracle 行转列~列转行(几种方法) 

    Oracle行转列、列转行的Sql语句总结

    Oracle 11g Pivot函数实现列转行

      

  • 相关阅读:
    詩与胡說张爱玲
    dragscroll 鼠标拖动滚动页面 userjs
    鼠标拖动控制页面滚动 (可运用于全屏触摸设备)
    给Webbrowser 添加用户脚本
    .net 4 环境调用 .net 2 编译的DLL
    webservice远程测试显示 “测试窗体只能用于来自本地计算机的请求”
    解决WebBrowser.DocumentCompleted事件的多次调用
    能够播放地球上现有的几乎所有格式 - VLC – 支持鼠标手势的播放器
    使 WebBrowser 更简单的插入、调用执行脚本, 可安装 jQuery 脚本的 C# 开源代码 IEBrowser
    “暂时没有对您所在的地区提供下载和试听服务”的解决办法
  • 原文地址:https://www.cnblogs.com/iupoint/p/10997254.html
Copyright © 2011-2022 走看看