I have researched & tried to solve this, but can't get through this. Please pardon me if this seems a simple one I have a dataset like below :-
I want to update all the rows for columns Inspection_Date and Sl_No as the latest within a gro up of SFID.
The resulting table should look like below :-
I have tried it in two ways:-
Help Source :- help from this sqlite: update all rows in group with value from first row
Code 1:-
UPDATE new_copy_2 set Sl_No = (
SELECT MAX(Sl_No) as max_date
FROM new_copy_2 T2
where Inspection_Type like '%PMCR%'
and exists
(select 1 from new_copy_2 tt
where tt.SFID = T2.SFID
and tt.Inspection_Type like '%PMCR%')
Error :-
Incorrect syntax near ')'.
Help Source :- MySQL - updating all records to match max value in group
Code 2:-
UPDATE new_copy_2
JOIN (
SELECT SFID, MAX(Sl_No) AS flag
FROM new_copy_2
GROUP BY
SFID
) t
ON SFID = t.SFID
SET Sl_No = t.flag
where Inspection_Type like '%PMCR%'
and exists
(select 1 from new_copy_2 tt
where tt.SFID = SFID
and tt.Inspection_Type like '%PMCR%')
Error:-
Incorrect syntax near the keyword 'JOIN'.
Incorrect syntax near 't'.
Please Help !!
Input data set example
| : Inspection_Date : | : SFID : | Sl_No
| : 7/4/2020 : | : SF-0000000HYD : | 8868
| : 6/4/2020 : | : SF-0000000HYD : | 8864
| : 3/3/2020 : | : SF-0000836659 : | 7845
| : 3/1/2020 : | : SF-0000836659 : | 7842
| : 4/7/2020 : | : SF-0000836432 : | 7862
| : 4/5/2020 : | : SF-0000836432 : | 7840
Expected data set example
|: Inspection_Date |: SFID |: Sl_No
|: 7/4/2020 |: SF-0000000HYD |: 8868
|: 7/4/2020 |: SF-0000000HYD |: 8868
|: 3/3/2020 |: SF-0000836659 |: 7845
|: 3/3/2020 |: SF-0000836659 |: 7845
|: 4/7/2020 |: SF-0000836432 |: 7862
|: 4/7/2020 |: SF-0000836432 |: 7862