Advertisement
Guest User

Postgres Query

a guest
Sep 8th, 2015
183
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. DO $$DECLARE r RECORD;
  2. BEGIN
  3. FOR r IN SELECT rate_type, id_customer, id_market, cic_ocn, id_jurisdiction
  4. FROM bulk.rates_contract
  5. WHERE id_customer IS NOT NULL
  6. AND end_date IS NULL
  7. GROUP BY 1,2,3,4,5
  8. HAVING COUNT(*) > 1 -- only retrieve a record if there is more than one with no end date
  9. LOOP
  10. RAISE NOTICE '%,%,%,%,%,%',timeofday(),r.rate_type, r.id_customer, r.id_market, r.cic_ocn, r.id_jurisdiction;
  11. -- put an UPDATE here for this combination to set the end date
  12. UPDATE bulk.rates_contract as rc
  13. SET end_date = (select min(rc2.eff_date)
  14. from bulk.rates_contract as rc2
  15. WHERE rc.eff_date < rc2.eff_date
  16. AND rc.rate_type = rc2.rate_type
  17. AND rc.id_customer = rc2.id_customer
  18. AND rc.id_market = rc2.id_market
  19. AND rc.cic_ocn = rc2.cic_ocn
  20. AND rc.id_jurisdiction = rc2.id_jurisdiction
  21. )
  22. WHERE rc.end_date is null
  23. AND rc.rate_type = r.rate_type
  24. AND rc.id_customer = r.id_customer
  25. AND rc.id_market = r.id_market
  26. AND rc.cic_ocn = r.cic_ocn
  27. AND rc.id_jurisdiction = r.id_jurisdiction
  28. ;
  29. END LOOP;
  30. END$$;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement