There is a table of currencies rates in MS SQL Server 2005:
ID | CURR | RATE | DATE
1 | USD | 30 | 01.10.2010
3 | GBP | 45 | 07.10.2010
5 | USD | 31 | 08.10.2010
7 | GBP | 46 | 09.10.2010
9 | USD | 32 | 12.10.2010
11 | GBP | 48 | 03.10.2010
Rate are updated in real time and there are more than 1 billion rows in the table.
It needs to write a SQL-query, wich will provide latest rates per each currency.
My decision is:
SELECT c.[id],c.[curr],c.[rate],c.[date]
FROM [curr_rate] c, (SELECT curr, MAX(date) AS rate_date FROM [curr_rate]
GROUP BY curr) t
WHERE c.date = t.rate_date AND c.curr = t.curr
ORDER BY c.[curr] ASC
Is it possible to write a query without sub-queries and join's with derived tables?