这是原始表的数据,一共是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个,即一个子查询查两个字段,因为查询的条件都是一样的。求大神指点!
数据汇总的子查询合并优化问题
- 写回答
- 好问题 0 提建议
- 追加酬金
- 关注问题
- 邀请回答
-
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解决 无用评论 打赏 举报