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 matlab生成电测深三层曲线模型代码
  • ¥50 随机森林与房贷信用风险模型
  • ¥50 buildozer打包kivy app失败
  • ¥30 在vs2022里运行python代码
  • ¥15 不同尺寸货物如何寻找合适的包装箱型谱
  • ¥15 求解 yolo算法问题
  • ¥15 虚拟机打包apk出现错误
  • ¥15 用visual studi code完成html页面
  • ¥15 聚类分析或者python进行数据分析
  • ¥15 三菱伺服电机按启动按钮有使能但不动作