Forum Discussion
Jlallmahomed
May 07, 2024Copper Contributor
Assi9stance in a Sql statement
Hi all I have a Table and I want to retrieve the record with the latest Sequence for each code. I need some help to write a query. Thanks to help me out. Code Name Sequence ...
olafhelper
May 08, 2024Bronze Contributor
Jlallmahomed , an easy one, aggregate on "Code" to get the MAX = highest sequence.
;WITH cte AS
(SELECT Code, MAX(Sequence) AS MaxSequence
FROM yourTable
GROUP BY Code)
SELECT T.*
FROM yourTable AS T
INNER JOIN
cte
ON T.Code = cte.Code
AND t.Sequence = cte.MaxSequence