oracle - sql-query that change all validTo dates to the next validFrom date minus one Day -


i have modify big pricelist table there 1 valid price every article. sales employees insert new prices , forgot change old infinite validto dates.

so have write sql-query change validto dates next validfrom date minus 1 day, when validto date has infinite validity (9999-12-31). have no idea how can reach sql (oracle 12).

anr price   validfrom   validto 1   447.1     2015-06-01  9999-12-31 < 1   447.2       2015-06-16  2015-06-16 1   447.3       2015-06-17  2015-06-17 1   447.4       2015-06-22  2015-06-22 1   447.5       2015-07-06  9999-12-31 < 1   395.0       2015-07-20  2015-07-20 1   447.6       2015-08-03  9999-12-31 < 1   447.7       2015-08-17  9999-12-31 < 1   447.8       2015-08-24  9999-12-31 < 1   395.0       2015-09-07  2015-09-07 1   450.9       2015-11-15  9999-12-31 < no change because last entry 

after updating the table, result should like

anr price   validfrom   validto 1   447.1       2015-06-01  2015-06-15 < 1   447.2       2015-06-16  2015-06-16 1   447.3       2015-06-17  2015-06-17 1   447.4       2015-06-22  2015-06-22 1   447.5       2015-07-06  2015-07-19 < 1   395.0       2015-07-20  2015-07-20 1   447.6       2015-08-03  2015-08-16 < 1   447.7       2015-08-17  2015-08-23 < 1   447.8       2015-08-24  2015-09-06 < 1   395.0       2015-09-07  2015-09-07 1   450.9       2015-11-15  9999-12-31 < 

in order update end date can select minimum of higher start dates.

update mytable upd set enddate = coalesce( (   select min(startdate) - 1   mytable later   later.startdate > upd.startdate   , later.anr = upd.anr -- same product ), date'9999-12-31') -- coalesce case there no later record enddate = date'9999-12-31'; 

i have taken anr product id. if isn't change statement accordingly.


Comments

Popular posts from this blog

javascript - jQuery: Add class depending on URL in the best way -

caching - How to check if a url path exists in the service worker cache -

Redirect to a HTTPS version using .htaccess -