I have a SQL Server
table with the following data:
I need to get the latest records for each currency so the result should be:
Is it possible to get this result in just one select without doing nested selects?
I have a SQL Server
table with the following data:
I need to get the latest records for each currency so the result should be:
Is it possible to get this result in just one select without doing nested selects?
You can use ORDER BY
with TIES
as follows:
SELECT TOP 1 WITH TIES *
FROM Src
ORDER BY ROW_NUMBER() OVER (PARTITION BY Currency ORDER BY ID DESC)
You could use the window functions
;with cteBase as (
Select *,RowNr=Row_Number() over(Partition By Currency Order By Date Desc) from YourTable
)
Select * from cteBase Where RowNr=1
since your records are entered at the day of RATE update(seams that way), this should work... (please modify the limit as to match the number of currencies in your table)
SELECT ID,DISTINCT(CURRENCY),DATE,RATE FROM table ORDER BY CURRENCY,-DATE LIMIT 0,3