需求:1.求出4.7号eicuser.E_MP_READ_CURVE 表中有数据而eicuser.R_CONS_E_STAT_CURVE为null的o.id
2.列出满足条件1的o.id在eicuser.E_MP_READ_CURVE 表中4.6号和4.7号的数据(r1,r2)
select o.id
h1.r1,
h2.r2,
h2.r1,
h2.r2
from eicuser.r_coll_obj o,
eicuser.E_MP_READ_CURVE h1,
eicuser.E_MP_READ_CURVE h2
where o.obj_mped_id = h1.id
and h1.data_type = 1
and h1.data_date = to_date('2015-04-06', 'yyyy-mm-dd')
and o.obj_mped_id = h2.id
and h2.data_type = 1
and h2.data_date = to_date('2015-04-07', 'yyyy-mm-dd')
not exists
(select g.cons_id
from eicuser.R_CONS_E_STAT_CURVE g
where g.cons_id = o.obj_cons_id
and g.data_date = to_date('2015-04-07', 'yyyy-mm-dd'))
------解决思路----------------------
不知道你的表结构,也不知道是否表结果一定要这样,发现你少了个AND
select o.id
h1.r1,
h2.r2,
h2.r1,
h2.r2
from eicuser.r_coll_obj o,
eicuser.E_MP_READ_CURVE h1,
eicuser.E_MP_READ_CURVE h2
where o.obj_mped_id = h1.id
and h1.data_type = 1
and h1.data_date = to_date('2015-04-06', 'yyyy-mm-dd')
and o.obj_mped_id = h2.id
and h2.data_type = 1
and h2.data_date = to_date('2015-04-07', 'yyyy-mm-dd')
AND not exists
(select g.cons_id
from eicuser.R_CONS_E_STAT_CURVE g
where g.cons_id = o.obj_cons_id
and g.data_date = to_date('2015-04-07', 'yyyy-mm-dd'))
------解决思路----------------------
select o.id,
h1.r1,
h2.r2,
h2.r1,
h2.r2
from eicuser.r_coll_obj o,
eicuser.E_MP_READ_CURVE h1,
eicuser.E_MP_READ_CURVE h2
where o.obj_mped_id = h1.id
and h1.data_type = 1
and h1.data_date = to_date('2015-04-06', 'yyyy-mm-dd')
and o.obj_mped_id = h2.id
and h2.data_type = 1
and h2.data_date = to_date('2015-04-07', 'yyyy-mm-dd')
and
not exists
(select g.cons_id
from eicuser.R_CONS_E_STAT_CURVE g
where g.cons_id = o.obj_cons_id
and g.data_date = to_date('2015-04-07', 'yyyy-mm-dd'))
------解决思路----------------------
SELECT o.id,h2.r1,h2.r2
FROM eicuser.r_coll_obj o
JOIN eicuser.E_MP_READ_CURVE h2 ON h2.id=o.obj_mped_id
WHERE EXISTS(SELECT 1 FROM eicuser.E_MP_READ_CURVE h1 WHERE h1.id=o.obj_mped_id AND date_date=to_date('2015-04-07', 'yyyy-mm-dd'))
AND NOT EXISTS(SELECT 1 FROM eicuser.R_CONS_E_STAT_CURVE g WHERE g.id=o.obj_mped_id AND date_date=to_date('2015-04-07', 'yyyy-mm-dd'))
AND(h2.date_date=to_date('2015-04-06', 'yyyy-mm-dd') OR h2.date_date=to_date('2015-04-07', 'yyyy-mm-dd'))
不知道ORACLE能不能通过