Oracle行转列,列转行使用实例

-----1.行转换为列

select a.fworkcenter as 车间,F1||'-数量' as 类型, fspec as 规格 ,ftype as 前缀

, to_char(fdate,'YYYY-MM-dd') as 日期, (case when a.fcode in (900,901) then to_char(fcount,'fm90.990') else cast(fcount as varchar(20)) end) 值 ,

a.fname,a.f2 as 厚度

from avg_resistance a

where 1=1 and a.fdate>=:STARTDATE and a.fdate<=:ENDDATE and length(a.ftype)=5 )t --and a.f1='天'

pivot ( max( 值 ) ----随便给个函数即可

for fname in

(

'合计' as "合计",

'电阻(<0.2)' as "电阻(小于0。2)",

'电阻(0.2,0.3)' as "电阻(0。2,0。3)",

'电阻(0.3,0.4)' as "电阻(0。3,0。4)",

'均值' as "均值"

) )

where 1=1 ;

-----2.列行转换

select *

from ( select a.containername,

(case when nvl(a.苹果,0)>0 then 苹果else 0 end ) as 苹果

(case when nvl(a.雪梨,0)>0 then 雪梨 else 0 end ) as 雪梨

(case when nvl(a.香蕉,0)>0 then 香蕉 else 0 end ) as 香蕉,

(case when nvl(a.龙眼,0)>0 then 龙眼 else 0 end ) as 龙眼,

a.cuworkorder

from cucontainergraderrecord a

where a.recordtime>=to_date('2023-10-01','yyyy-mm-dd')

and a.containername like 'Z%'

and a.specname='分选')t

unpivot

(

fcount ---(原表的列对应的值)

for fitem ----(原表列名称,多少列,就变多少行, oracle的直接用列名称,不用',''等)

in ( 苹果,雪梨,香蕉,龙眼

)

)

相关推荐
东南门吹雪17 分钟前
Spring事务传播机制深度解析
java·数据库·spring
不甘先生18 分钟前
PostgreSQL 中的 JSONB 详解:从入门到实战
数据库·postgresql
Irene199123 分钟前
PL/SQL:异常处理补充
数据库·sql
dishugj24 分钟前
SAP HANA数据库文件目录说明
服务器·数据库·oracle
l1t27 分钟前
DeepSeek总结的使用 eBPF 和硬件断点跟踪 PostgreSQL
数据库·驱动开发·postgresql
薪火铺子38 分钟前
MySQL InnoDB 索引底层:B+树深度解析
数据库·b树·mysql
Elastic 中国社区官方博客43 分钟前
从平均值到任意百分位数:Elasticsearch 在 ES|QL 中原生支持指数直方图
大数据·数据库·sql·elasticsearch·搜索引擎·全文检索·prometheus
今儿敲了吗1 小时前
数据库(四)——关系数据库SQL语言
数据库·笔记·sql
brevity_souls1 小时前
SQL server格式化日期
运维·服务器·数据库
虹科网络安全1 小时前
艾体宝干货|Active-Active/Active-Passive 数据库架构解析:高可用设计中的权衡与选型
数据库·数据库架构