March 8, 2018 at 1:01 am
Hi , I have a scenario and need some idea to build that.
i have a table where multiple stat-codes exist with different part-no with diff dates.I need to Get the maximum dates on each stat-codes and update.
exp:
STAT_CODE,DATE,PART_NO
A123,2018-02-02,101
A123,2018-02-01,201
A123,2018-01-02,301
AB22,2017-02-09,201
AB22,2017-02-03,401
AB22,2017-02-04,501
AB22,2017-02-03,101
My Output should be,
A123,2018-02-02,101
A123,2018-02-02,201
A123,2018-02-02,301
AB22,2017-02-04,501
AB22,2017-02-04,401
Here i want the STAT_CD and the highest date and based on that i will update that date for all Part no
March 8, 2018 at 4:42 am
update table a
set a.date=(select max(b.date) from table b where b.stat_cd=a.stat_cd)
March 8, 2018 at 5:03 am
This may work, although I don't understand why the date for AB22 shouldn't be 2017-02-09?
WITH MaxDates AS (
SELECT
STAT_CODE
, DATE
, PART_NO
, MAX(DATE) OVER (PARTITION BY STAT_CODE)) AS MaxDate
)
UPDATE MaxDates
SET DATE = MaxDate
John
March 8, 2018 at 7:10 am
skmoh2 - Thursday, March 8, 2018 1:01 AMHi , I have a scenario and need some idea to build that.i have a table where multiple stat-codes exist with different part-no with diff dates.I need to Get the maximum dates on each stat-codes and update.
exp:
STAT_CODE,DATE,PART_NO
A123,2018-02-02,101
A123,2018-02-01,201
A123,2018-01-02,301AB22,2017-02-09,201
AB22,2017-02-03,401
AB22,2017-02-04,501
AB22,2017-02-03,101
My Output should be,
A123,2018-02-02,101
A123,2018-02-02,201
A123,2018-02-02,301
AB22,2017-02-04,501
AB22,2017-02-04,401Here i want the STAT_CD and the highest date and based on that i will update that date for all Part no
You'll need to be very specific as to why all the A123 records get the February 2nd date, but the AB22 records end up with 2 missing records, and use February 4th instead of February 9th.
Steve (aka sgmunson) 🙂 🙂 🙂
Rent Servers for Income (picks and shovels strategy)
Viewing 4 posts - 1 through 3 (of 3 total)
You must be logged in to reply to this topic. Login to reply