yhnmjkl 2017-07-03 09:37 采纳率: 0%
浏览 775

数据汇总的子查询合并优化问题

图片说明
这是原始表的数据,一共是4列,分别是日期,油田ID,产液量,产油量
我的需求是查询出每天每个油田的产液量、产油量、当月累计产液量、当月累计产油量、当年累计产液量、当年累计产油量
下边是我写的sql语句:
select t.the__date,t.id__field,t.liquid,t.test__oil,__
--月累计
(select sum(t1.liquid) from TJ_FIELD_PROD_DAILY t1 where t1.id_field=t.id_field and t1.thedate<=t.thedate and t1.thedate>=(select trunc(t.thedate,'mm') from dual)),
(select sum(t1.test_oil) from TJ_FIELD_PROD_DAILY t1 where t1.id_field=t.id_field and t1.thedate<=t.thedate and t1.thedate>=(select trunc(t.thedate,'mm') from dual)),
--年累计
(select sum(t1.liquid) from TJ_FIELD_PROD_DAILY t1 where t1.id_field=t.id_field and t1.thedate<=t.thedate and t1.thedate>=(select trunc(t.thedate,'yyyy') from dual)),
(select sum(t1.test_oil) from TJ_FIELD_PROD_DAILY t1 where t1.id_field=t.id_field and t1.thedate<=t.thedate and t1.thedate>=(select trunc(t.thedate,'yyyy') from dual))
from TJ_FIELD_PROD_DAILY t
这样一共写了4个子查询,查询效率很慢,我想问有没有办法可以把这4个子查询合并成2个,即一个子查询查两个字段,因为查询的条件都是一样的。求大神指点!

  • 写回答

2条回答

  • __如影 2017-07-04 01:59
    关注

    select t.the__date,t.id__field,t.liquid,t.test__oil,__
    --月累计
    (select sum(t1.liquid),sum(t1.test_oil) from TJ_FIELD_PROD_DAILY t1 where t1.id_field=t.id_field and t1.thedate<=t.thedate and t1.thedate>=(select trunc(t.thedate,'mm') from dual)),
    --年累计
    (select sum(t1.liquid),sum(t1.test_oil) from TJ_FIELD_PROD_DAILY t1 where t1.id_field=t.id_field and t1.thedate<=t.thedate and t1.thedate>=(select trunc(t.thedate,'yyyy') from dual))
    from TJ_FIELD_PROD_DAILY t

    评论

报告相同问题?

悬赏问题

  • ¥15 求daily translation(DT)偏差订正方法的代码
  • ¥15 js调用html页面需要隐藏某个按钮
  • ¥15 ads仿真结果在圆图上是怎么读数的
  • ¥20 Cotex M3的调试和程序执行方式是什么样的?
  • ¥20 java项目连接sqlserver时报ssl相关错误
  • ¥15 一道python难题3
  • ¥15 牛顿斯科特系数表表示
  • ¥15 arduino 步进电机
  • ¥20 程序进入HardFault_Handler
  • ¥15 关于#python#的问题:自动化测试