I am trying to get the value of some data from the following example table:
DEAL_ID | DISC | EFF_DATE | EXP_DATE
100 | 2 | 01-01-19 | 31-01-2019
101 | 2 | 01-02-19 | 15-02-2019
103 | 3 | 01-03-19 | 31-03-2019
104 | 4 | 01-04-19 | 25-04-2019
As seen, there is a time period between deal_id 101 and 103, from 16-2-2019 to 28-2-2019 that has no assigned DISC value. In this case, for any period that has no assigned DEAL/DISC, it would take the last EXP_DATE that exists beforethat time period, and change the EXP_DATE to the next EFF_DATE, covering all the time period that didn't have an asssigned one, ending the table like this:
DEAL_ID | DISC | EFF_DATE | EXP_DATE
100 | 2 | 01-01-19 | 31-01-2019
101 | 2 | 01-02-19 | 28-02-2019
103 | 3 | 01-03-19 | 31-03-2019
104 | 4 | 01-04-19 | 25-04-2019
I did find there is a LEAD/LAG function for DB2, however, it is not valid/doesn't work/not recognized in the version I am using (am not sure which version is, btw).
The current code I have is:
SELECT A.deal_id as deal_id, A.vendr_nbr, A.vndr_deal_eff_date, A.vndr_deal_exp_date, (YEAR(vndr_deal_eff_date) * 10000 + MONTH(vndr_deal_eff_date)*100 + DAY(vndr_deal_eff_date)) limitDate1EFF, (YEAR(vndr_deal_exp_date) * 10000 + MONTH(vndr_deal_exp_date)*100 + DAY(vndr_deal_exp_date)) limitDate2EXP, B.DISC_CHRG_ID, B.DISCOUNT_VALUE, C.VENDR_DEPT_NBR
FROM workdbo.vendor_deal A
LEFT JOIN workdbo.DDA_DISC_CHRG_TIER B ON A.deal_id = B.DISC_CHRG_ID
LEFT JOIN workdbo.DEAL_DEPT C ON A.deal_id = C.deal_id
Any help would be appreciated.
Note: EFF_DATE and EXP_DATE are both date
data type, and are recognized as such. From the query posted I have, A.vndr_deal_eff_date, A.vndr_deal_exp_date
are the dates I am using.
(If the post needs changing to fit stackoverflow guidelines, please do tell what should I edit/add/etc, instead of just leaving a -1, thanks)
Based on your "example" table, you should just use lead()
:
select . . . ,
add_days(lead(eff_date) over (order by eff_date), -1)
from example;
I'm not sure what your query -- which mentions 3 tables and doesn't seem to use a date
data type -- has to do with the question.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.