Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- DO $$DECLARE r RECORD;
- BEGIN
- FOR r IN SELECT rate_type, id_customer, id_market, cic_ocn, id_jurisdiction
- FROM bulk.rates_contract
- WHERE id_customer IS NOT NULL
- AND end_date IS NULL
- GROUP BY 1,2,3,4,5
- HAVING COUNT(*) > 1 -- only retrieve a record if there is more than one with no end date
- LOOP
- RAISE NOTICE '%,%,%,%,%,%',timeofday(),r.rate_type, r.id_customer, r.id_market, r.cic_ocn, r.id_jurisdiction;
- -- put an UPDATE here for this combination to set the end date
- UPDATE bulk.rates_contract as rc
- SET end_date = (select min(rc2.eff_date)
- from bulk.rates_contract as rc2
- WHERE rc.eff_date < rc2.eff_date
- AND rc.rate_type = rc2.rate_type
- AND rc.id_customer = rc2.id_customer
- AND rc.id_market = rc2.id_market
- AND rc.cic_ocn = rc2.cic_ocn
- AND rc.id_jurisdiction = rc2.id_jurisdiction
- )
- WHERE rc.end_date is null
- AND rc.rate_type = r.rate_type
- AND rc.id_customer = r.id_customer
- AND rc.id_market = r.id_market
- AND rc.cic_ocn = r.cic_ocn
- AND rc.id_jurisdiction = r.id_jurisdiction
- ;
- END LOOP;
- END$$;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement