oracle 分组后多行合并为一行


工作中有需求要将如下结构的数据(为方便处理数据类型均为字符换):

ID DATE VALUE
a 2016-10-01 1
a 2016-10-02 2
a 2016-10-03 3
b 2016-10-01 3
b 2016-10-02 2
b 2016-10-03 1

 

 

 

 

 

 

 

合并为如下样式:

ID V1 V2 V3
a 1 2 3
b 3 2 1

 

 

 

 

SQL如下:

select t.id,

  max(case(t.date) when '2016-10-01' then t.value end) as v1,

  max(case(t.date) when '2016-10-02' then t.value end) as v2,

  max(case(t.date) when '2016-10-03' then t.value end) as v3

from table t

group by t.id;

----------------------------------------------------------------------

执行结果很完美!!!

 


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM