数据库----Oracle字段横置

背景需要

  最近好久没有写文章了,主要原因因为项目试运行了,自己每天都要关注项目运行情况,在稳定的时候去开发新的功能,但是在开发的时候,遇到一个问题难倒我了。具体的原因是,有时候客户想要的数据是一条记录展示完,但是我们在数据库存的时候,通常都是一个存一条记录。这就会有一个问题,什么问题呢,比方说,客户想要这台设备24小时,每个小时的产量,以这样的形式展示,
在这里插入图片描述
但是设备每个小时的产量在我们数据库中都对应了一条记录,是这样的
在这里插入图片描述
我现在知道下面这所有的记录,对应的单小时产量应该填在每一个时间段中,那么我该怎么做到?

解决方案

当时这个问题也是困扰了自己好长时间,本来自己写了sql,结果发现总会出现一点问题,最后在网上找到比较好的解决方案,最后完美解决客户需要,主要使用的函数,case when
最后的sql为:

select a.orderchildid,a.deviceid,
sum(case a.datehour when '00' then a.quantity else null end )as t0,
sum(case a.datehour when '01' then a.quantity else null end )as t1,
sum(case a.datehour when '02' then a.quantity else null end )as t2,
sum(case a.datehour when '03' then a.quantity else null end )as t3,
sum(case a.datehour when '04' then a.quantity else null end )as t4,
sum(case a.datehour when '05' then a.quantity else null end )as t5,
sum(case a.datehour when '06' then a.quantity else null end )as t6,
sum(case a.datehour when '07' then a.quantity else null end )as t7,
sum(case a.datehour when '08' then a.quantity else null end )as t8,
sum(case a.datehour when '09' then a.quantity else null end )as t9,
sum(case a.datehour when '10' then a.quantity else null end )as t10,
sum(case a.datehour when '11' then a.quantity else null end )as t11,
sum(case a.datehour when '12' then a.quantity else null end )as t12,
sum(case a.datehour when '13' then a.quantity else null end )as t13,
sum(case a.datehour when '14' then a.quantity else null end )as t14,
sum(case a.datehour when '15' then a.quantity else null end )as t15,
sum(case a.datehour when '16' then a.quantity else null end )as t16,
sum(case a.datehour when '17' then a.quantity else null end )as t17,
sum(case a.datehour when '18' then a.quantity else null end )as t18,
sum(case a.datehour when '19' then a.quantity else null end )as t19,
sum(case a.datehour when '20' then a.quantity else null end )as t20,
sum(case a.datehour when '21' then a.quantity else null end )as t21,
sum(case a.datehour when '22' then a.quantity else null end )as t22,
sum(case a.datehour when '23' then a.quantity else null end )as t23
from biz_dev_order_phq a
where a.shiftdate=to_char(sysdate,'yyyy-MM-dd')
group by a.shiftdate,a.deviceid,a.orderchildid;

执行结果如下:
在这里插入图片描述

case when介绍
语法
  CASE search_expression

  WHEN expression1 THEN result1

  WHEN expression2 THEN result2

  ...

  WHEN expressionN THEN resultN

  ELSE default_result

  通常用时做条件判断,根绝不同情况返回不同的结果,最简单的方法就是实际与理想出现偏差的例子,
比如说,每个人都有一个每天都会响一次的闹钟,必须要设定个时间,如果你没有设定时间,那么系统就给你一个默认的每天早上8点响(只是例子),这个时候可能用nvl函数更好,但是case when不仅限于为空,可以是满足某个条件,比如通过表达式可以起到,定在0:00-6:00的我可以给他的结果赋为7:00。这就是case when的好处。


补充

后来也是发现了oracle中有一个专门这样的函数,当时做完忘记更新了,大家如果感兴趣的话可以关注一下oracle里面的这个函数 pivot

点我直达

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值