SQL Clone
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


Finding Max and update


Finding Max and update

Author
Message
skmoh2
skmoh2
SSC-Addicted
SSC-Addicted (423 reputation)SSC-Addicted (423 reputation)SSC-Addicted (423 reputation)SSC-Addicted (423 reputation)SSC-Addicted (423 reputation)SSC-Addicted (423 reputation)SSC-Addicted (423 reputation)SSC-Addicted (423 reputation)

Group: General Forum Members
Points: 423 Visits: 68
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


george_at_sql
george_at_sql
Right there with Babe
Right there with Babe (767 reputation)Right there with Babe (767 reputation)Right there with Babe (767 reputation)Right there with Babe (767 reputation)Right there with Babe (767 reputation)Right there with Babe (767 reputation)Right there with Babe (767 reputation)Right there with Babe (767 reputation)

Group: General Forum Members
Points: 767 Visits: 1288

update table a
set a.date=(select max(b.date) from table b where b.stat_cd=a.stat_cd)

John Mitchell-245523
John Mitchell-245523
SSC Guru
SSC Guru (121K reputation)SSC Guru (121K reputation)SSC Guru (121K reputation)SSC Guru (121K reputation)SSC Guru (121K reputation)SSC Guru (121K reputation)SSC Guru (121K reputation)SSC Guru (121K reputation)

Group: General Forum Members
Points: 121330 Visits: 18756
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
sgmunson
sgmunson
SSC Guru
SSC Guru (74K reputation)SSC Guru (74K reputation)SSC Guru (74K reputation)SSC Guru (74K reputation)SSC Guru (74K reputation)SSC Guru (74K reputation)SSC Guru (74K reputation)SSC Guru (74K reputation)

Group: General Forum Members
Points: 74104 Visits: 6352
skmoh2 - Thursday, March 8, 2018 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


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)
Smile Smile Smile
Health & Nutrition
Go


Permissions

You can't post new topics.
You can't post topic replies.
You can't post new polls.
You can't post replies to polls.
You can't edit your own topics.
You can't delete your own topics.
You can't edit other topics.
You can't delete other topics.
You can't edit your own posts.
You can't edit other posts.
You can't delete your own posts.
You can't delete other posts.
You can't post events.
You can't edit your own events.
You can't edit other events.
You can't delete your own events.
You can't delete other events.
You can't send private messages.
You can't send emails.
You can read topics.
You can't vote in polls.
You can't upload attachments.
You can download attachments.
You can't post HTML code.
You can't edit HTML code.
You can't post IFCode.
You can't post JavaScript.
You can post emoticons.
You can't post or upload images.

Select a forum








































































































































































SQLServerCentral


Search