SQL-query task, decision?

Posted by Sirius Lampochkin on Stack Overflow See other posts from Stack Overflow or by Sirius Lampochkin
Published on 2010-04-01T08:18:04Z Indexed on 2010/04/01 8:23 UTC
Read the original article Hit count: 355

Filed under:
|
|
|
|

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?

© Stack Overflow or respective owner

Related posts about sql

Related posts about sql-server-2005