2016 - 2024

感恩一路有你

oracle动态列名 oracle动态的行转列怎么搞?

浏览量:1238 时间:2021-03-17 13:34:43 作者:admin

oracle动态的行转列怎么搞?

用union all

假设列名分别为 col1 cola colb...

select col1,cola

from tabname

where ...

union all

select col1,colb

from tabname

where ...

union all

select col1,colc

from tabname

where ...

union all

select col1,cold

from tabname

where ...

union all

select col1,cole

from tabname

where ...

union all

select col1,colf

from tabname

where ...

Oracle列转行,行转列?

oracle下可以用函数decode处理:

select 产品名称,

sum(decode(季度,"第一季度",销售额,0)) 第一季度销售额,

sum(decode(季度,"第二季度",销售额,0)) 第二季度销售额,

sum(decode(季度,"第三季度",销售额,0)) 第三季度销售额,

sum(decode(季度,"第四季度",销售额,0)) 第四季度销售额,

from 表名

group by 产品名称

Oracle实现行转换成列的方法?

数据表示例:假设要将name值作为行,course值作为列,转换后效果为:对应的SQL如下:方法1:使用表连接SELECT DISTINCT a.name,(SELECT score FROM grade b WHEREa.name=b.name AND b.course="语文") AS "语文",(SELECT score FROM grade b WHEREa.name=b.name AND b.course="数学") AS "数学",(SELECT score FROM grade b WHEREa.name=b.name AND b.course="英语") AS "英语"FROM grade a方法2:使用分组SELECT name,SUM(CASE course WHEN "语文" THEN score END) AS "语文",SUM(CASE course WHEN "数学" THEN score END) AS "数学",SUM(CASE course WHEN "英语" THEN score END) AS "英语"FROM grade GROUP BY name

oracle动态列名 oracle中行转列 oracle行转列pivot动态

版权声明:本文内容由互联网用户自发贡献,本站不承担相关法律责任.如有侵权/违法内容,本站将立刻删除。