insert into con_flush_record
(wellname, record_date, unitcode, start_time, range_time, in_water, out_water, displacement, reason, type_id, created_on)
select jh, rq, dwdm, xjrq, xjys, jksl, cksl, jkpl, xjyy, xjfs, created_on
from (select t.*, row_number() over(partition by jh, rq order by xjrq desc) rn
from (select jh, trunc(xjrq) rq, dwdm, xjrq, xjys, jksl, cksl, jkpl, xjyy, xjfs, sysdate created_on
from YS_DGC12 t
where xjrq >= #{startDate} and xjrq < #{endDate} + 1
and not exists (select 1 from CON_FLUSH_RECORD where wellname = t.jh and record_date = trunc(t.xjrq))
order by jh, xjrq desc) t) t where rn = 1
insert into CON_FLUSH_DAILY
(WELLNAME,
RECORD_DATE,
UNITCODE,
RANGE_TIME,
DISPLACEMENT,
REASON,
TYPE_ID,
EFFECTIVE,
CREATED_ON,
MODIFIED_ON,
before_date,
BEFORE_PT,
BEFORE_PLAN_WATER,
BEFORE_WATER,
after_date,
AFTER_PT,
AFTER_PLAN_WATER,
AFTER_WATER,
AFTER_WATER_RATE)
SELECT WELLNAME,
RECORD_DATE,
UNITCODE,
RANGE_TIME,
DISPLACEMENT,
REASON,
TYPE_ID,
EFFECTIVE,
CREATED_ON,
MODIFIED_ON,
before_date,
A.YY BEFORE_PT,
A.RPZSL BEFORE_PLAN_WATER,
A.RZSL BEFORE_WATER,
after_date,
B.YY AFTER_PT,
B.RPZSL AFTER_PLAN_WATER,
B.RZSL AFTER_WATER,
DECODE(B.RPZSL, 0, NULL, ROUND(B.RZSL / B.RPZSL, 2)) AFTER_WATER_RATE
from
(SELECT t.*,
(select max(rq) from YS_DBA02 where jh = t.wellname and rq < t.record_date and scsj = 24) before_date,
(select min(rq) from YS_DBA02 where jh = t.wellname and rq > t.record_date and scsj = 24) after_date
FROM CON_FLUSH_RECORD T
where not exists (select 1 from CON_FLUSH_DAILY where wellname = t.wellname and record_date = t.record_date)
) t
LEFT JOIN YS_DBA02 A ON T.WELLNAME = A.JH AND T.before_date = A.RQ
LEFT JOIN YS_DBA02 B ON T.WELLNAME = B.JH AND T.after_date = B.RQ
merge into CON_FLUSH_DAILY a
using (
select WELLNAME,
RECORD_DATE,
after_date2,
B.YY AFTER_PT,
B.RPZSL AFTER_PLAN_WATER,
B.RZSL AFTER_WATER,
DECODE(B.RPZSL, 0, NULL, ROUND(B.RZSL / B.RPZSL, 2)) AFTER_WATER_RATE
from
(SELECT t.*,
(select min(rq) from YS_DBA02 where jh = t.wellname and rq > t.record_date and scsj = 24) after_date2
FROM CON_FLUSH_DAILY T
where after_date is null
) t
JOIN YS_DBA02 B ON T.WELLNAME = B.JH AND T.after_date2 = B.RQ
) t
on (a.WELLNAME = t.WELLNAME and a.RECORD_DATE = t.RECORD_DATE)
when matched then
update set
a.after_date = t.after_date2,
a.AFTER_PT = t.AFTER_PT,
a.AFTER_PLAN_WATER = t.AFTER_PLAN_WATER,
a.AFTER_WATER = t.AFTER_WATER,
a.AFTER_WATER_RATE = t.AFTER_WATER_RATE