sql - Update/insert missing date ranges from new data? -


below tablea present in oracle database.

item    price   start_date  end_date xyz     256     1-jan-15    17-jan-15 xyz     125     16-mar-15   31-mar-15 

below new data client provided.

item    price   start_date  end_date xyz     236     5-jan-15    10-jan-15 xyz     253     15-jan-15   20-jan-15 xyz     253     25-jan-15   2-mar-15 

now need compare above new data tablea data , fill missing date ranges. if new data have date gap must taken tablea(if present). final updated tablea must below:

item    price   start_date  end_date xyz     256     1-jan-15    4-jan-15 xyz     236     5-jan-15    10-jan-15 xyz     256     11-jan-15   14-jan-15 xyz     253     15-jan-15   20-jan-15 xyz     253     25-jan-15   2-mar-15 xyz     125     3-mar-15    31-mar-15 

this need done in oracle using sql/plsql. please help.

seems can achieved using lead/lag functions not sure how include price field in query. thing below

select t.item, t.start_date,        lead(t.start_date) on (partition t.item order t.start_date)-1 end_dte,        t.price ((select item, start_date,end_date,price        xxa       ) union       (select item, start_date,end_date, price        xxb       )      ) t group t.item, t.start_date,t.price; 

you slect data tables, , select ones tablea matches same value in tableb.

for example:
select ones occur in both tables:

select  *     tablea ta, tableb tb   ta.item = tb.item  ,     ta.price = tb.price  ,     ta.start_date = ta.start_date  ,     ta.end_date = ta.end_date; 

Comments

Popular posts from this blog

Magento/PHP - Get phones on all members in a customer group -

php - Bypass Geo Redirect for specific directories -

php - .htaccess mod_rewrite for dynamic url which has domain names -