I am trying to be able to update the Pop' column in Table A with the New column value from Table B based on where the SDate for each row in Table A fits in the date history based on
LDate (i.e. the first column with SDate of 2019-01-12 is after LDate row with 2018-10-03 and before 2019-04-03 so the updated value for Pop in Table A would be the New value of D)
Table A
SDate Line Item Pop
2019-01-12 WIX WP10266 B
2019-01-30 WIX WP10266 B
2019-01-18 WIX WP10266 B
2019-01-17 WIX WP10266 B
2019-01-24 WIX WP10266 B
2019-01-02 WIX WP10266 B
Table B
LDate Line Item Oldval Newval
2019-10-03 WIX WP10266 C B
2019-04-03 WIX WP10266 D C
2018-10-03 WIX WP10266 W D
2018-09-06 WIX WP10266 N W
I started down the path of using CTE to get a single value but then realized I can't do an update using CTE,
I specified the line and item for speed while I was testing
with t1 as
(select SDate, line, item, pop
from table A
where backfill = 'Y' and line = 'WIX' and item = 'WP10266'),
t2 as
(select LDate, line, item, oldval, newval
from table B
where field = 'IPOPCD' and line = 'WIX' and item = 'WP10266')
select t1.SDate, t2.LDate, t1.line, t1.item, t2.oldval, t2.newval
from t1
join t2 on t1.line = t2.line and t1.item = t2.item
where t1.SDate >= t2.LDate;
which still leaves me multiple rows for each row in Table A
SDate LDate Line Item oldval newval
2019-01-12 2018-09-06 WIX WP10266 N W
2019-01-12 2018-10-03 WIX WP10266 W D
2019-01-30 2018-09-06 WIX WP10266 N W
2019-01-30 2018-10-03 WIX WP10266 W D
2019-01-18 2018-09-06 WIX WP10266 N W
2019-01-18 2018-10-03 WIX WP10266 W D
2019-01-17 2018-09-06 WIX WP10266 N W
2019-01-17 2018-10-03 WIX WP10266 W D
2019-01-24 2018-09-06 WIX WP10266 N W
2019-01-24 2018-10-03 WIX WP10266 W D
LDate (i.e. the first column with SDate of 2019-01-12 is after LDate row with 2018-10-03 and before 2019-04-03 so the updated value for Pop in Table A would be the New value of D)
Table A
SDate Line Item Pop
2019-01-12 WIX WP10266 B
2019-01-30 WIX WP10266 B
2019-01-18 WIX WP10266 B
2019-01-17 WIX WP10266 B
2019-01-24 WIX WP10266 B
2019-01-02 WIX WP10266 B
Table B
LDate Line Item Oldval Newval
2019-10-03 WIX WP10266 C B
2019-04-03 WIX WP10266 D C
2018-10-03 WIX WP10266 W D
2018-09-06 WIX WP10266 N W
I started down the path of using CTE to get a single value but then realized I can't do an update using CTE,
I specified the line and item for speed while I was testing
with t1 as
(select SDate, line, item, pop
from table A
where backfill = 'Y' and line = 'WIX' and item = 'WP10266'),
t2 as
(select LDate, line, item, oldval, newval
from table B
where field = 'IPOPCD' and line = 'WIX' and item = 'WP10266')
select t1.SDate, t2.LDate, t1.line, t1.item, t2.oldval, t2.newval
from t1
join t2 on t1.line = t2.line and t1.item = t2.item
where t1.SDate >= t2.LDate;
which still leaves me multiple rows for each row in Table A
SDate LDate Line Item oldval newval
2019-01-12 2018-09-06 WIX WP10266 N W
2019-01-12 2018-10-03 WIX WP10266 W D
2019-01-30 2018-09-06 WIX WP10266 N W
2019-01-30 2018-10-03 WIX WP10266 W D
2019-01-18 2018-09-06 WIX WP10266 N W
2019-01-18 2018-10-03 WIX WP10266 W D
2019-01-17 2018-09-06 WIX WP10266 N W
2019-01-17 2018-10-03 WIX WP10266 W D
2019-01-24 2018-09-06 WIX WP10266 N W
2019-01-24 2018-10-03 WIX WP10266 W D
Comment