这是原始表的数据,一共是4列,分别是日期,油田ID,产液量,产油量
我的需求是查询出每天每个油田的产液量、产油量、当月累计产液量、当月累计产油量、当年累计产液量、当年累计产油量
下边是我写的sql语句:
select t.thedate,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个,即一个子查询查两个字段,因为查询的条件都是一样的。求大神指点!