Reputation: 706
I have the following data in the table:
I have tried using the below query to get the latest CMBR_MBST
with its respective CMBR_CCMP
SELECT CMBR_CCMP, MAX(CMBR_MBST) AS CMBR_MBST
FROM CMBR
GROUP BY CMBR_CCMP
The result is close to what I need. I just need the extra 2 columns, CMBR_MMBR
& CMBR_MBED
to be displayed.
For example:
Can someone please help me with the query? Your help would be very much appreciated. Thanks
Upvotes: 0
Views: 65
Reputation: 93694
Here is one way using ROW_NUMBER
SELECT TOP 1 with ties *
FROM CMBR
Order by row_number()over(parition by CMBR_CCMP order by CMBR_MBST desc)
Another way (commonly used technique)
select * from
(
SELECT *,row_number()over(parition by CMBR_CCMP order by CMBR_MBST desc) Rn
FROM CMBR
) A
Where Rn =1
Upvotes: 1