shA, here is the final answer
--Sample table creation Script
create table sesht(CUSTID int,fromdate date,activity char(1));
--Sample data
insert into sesht values (1,'01-JAN-2015' ,'A') ;
insert into sesht values (1,'05-JAN-2015' ,'A') ;
insert into sesht values (1,'10-JAN-2015' ,'B') ;
insert into sesht values (1,'12-JAN-2015' ,'B') ;
insert into sesht values (1,'15-JAN-2015' ,'B') ;
insert into sesht values (1,'16-JAN-2015' ,'A') ;
insert into sesht values (1,'20-JAN-2015' ,'C') ;
insert into sesht values (1,'22-JAN-2015' ,'C') ;
insert into sesht values (1,'24-JAN-2015' ,'C') ;
----Base table - work out next record and see if the data is chagnged and rownumber is assigned to every record
drop table seshtt ;
create table seshtt as
Select custid,fromdate,COALESCE(activity,'') Activity
,CASE
WHEN COALESCE(activity,'') <> COALESCE(LEAD(activity) OVER (PARTITION BY CUSTID ORDER BY rn),'') THEN
LEAD(FromDate) OVER (PARTITION BY CUSTID ORDER BY FromDate)
END AS prevFromDate
,rn
from
(
Select custid,fromdate, activity, ROW_NUMBER() OVER (PARTITION BY CUSTID ORDER BY FromDate) rn from sesht
)
;
Select aa.custid,aa.fromdate,(NULL) TODATE,activity,aa.rn from seshtt aa
join
(
select custid,max(rn)+1 rn from seshttt
group by custid
) bb
on aa.custid=bb.custid
and aa.rn=bb.rn;
select * from seshttt ;
----Build main records (First & last records are not sorted)
create table seshttt as
SELECT custid, COALESCE(LAG(prevFromDate) OVER (PARTITION BY CUSTID ORDER BY fromdate), FromDate) FromDate, prevFromDate ToDate, Activity,rn
FROM seshtt
WHERE prevFromDate IS NOT NULL AND FromDate IS NOT NULL;
----Last record is now sorted & it becomes main table where first record will need to be updated
create table seshtttt as
Select * from seshttt
UNION ALL
Select aa.custid,aa.fromdate,(NULL) TODATE,activity,aa.rn from seshtt aa
join
(
select custid,max(rn)+1 rn from seshttt
group by custid
) bb
on aa.custid=bb.custid
and aa.rn=bb.rn;
select * from seshtttt;
---First record is sorted from both tables and rowid is used to merge the dataset;
create table seshtttttt as
Select src.*,bb.fromdate srcdate from
(
Select aa.custid,aa.fromdate,aa.rowid rwid from seshtttt aa join
(
Select custid,min(rn) rn from seshtttt group by custid
) bb
on aa.custid=bb.custid and aa.rn=bb.rn
) src
join
(
Select custid,fromdate from seshtt where rn=1
) bb
on src.custid=bb.custid;
select * from seshtttttt ;
---Merge runs here
merge into seshtttt tgt
using seshtttttt src
on
(tgt.rowid=src.rwid
and tgt.custid=src.custid)
when
matched then
update set tgt.fromdate=src.srcdate;
commit;
select * from seshtttt ;
1. List item
что такое изменение в действии column? как он рассчитывается? – Dawn