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函数实现列转行

      

  • 相关阅读:
    python中is和==的区别
    深拷贝和浅拷贝
    编码和解码
    with语句处理异常
    python中运行flask报错:UnicodeDecodeError: 'utf8' codec can't decodebyte 0xd5 in position 0:invalid continuation byte
    python中update的基本使用
    python中的程序控制结构
    python中的四种存储结构总结
    python中list,tuple,dict,set特点对比总结
    解决UIScrollview无故偏移和导航条遮挡view的问题
  • 原文地址:https://www.cnblogs.com/iupoint/p/10997254.html
Copyright © 2011-2022 走看看