Wrox Programmer Forums
|
SQL Server 2000 General discussion of Microsoft SQL Server -- for topics that don't fit in one of the more specific SQL Server forums. version 2000 only. There's a new forum for SQL Server 2005.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the SQL Server 2000 section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old January 26th, 2005, 10:10 AM
Registered User
 
Join Date: Jan 2005
Posts: 6
Thanks: 0
Thanked 0 Times in 0 Posts
Default can this be done in t-SQL?

I have a table that keeps exchange rates for multiple currencies:
fromCur, toCur, rate, effectiveDate
USD BRL .1 12/2/04
USD BRL .11 1/1/05
USD CAN .5 11/3/04 ...
and so on for all combinations.
I need to get the most recent rate for a given fromCur, for _EACH_ toCur and I can't think of a way to do it with out a procedural approach that:
1. Gets all ToCur's for a given fromCur (usd)
2. For each toCur, select top 1 rate from <table> where fromCur = 'usd' and toCur = @toCur order by effDate Desc

IS there a t-SQL only way to do this?
Thank you for any input.


 
Old January 26th, 2005, 10:43 AM
Friend of Wrox
 
Join Date: Jun 2003
Posts: 1,212
Thanks: 0
Thanked 1 Time in 1 Post
Default

You can do this wth a correlated subquery. Something like this

SELECT fromCur, toCur, rate, effectiveDate
FROM ExchangeRates er1
WHERE fromCur = @fromCur
AND effectiveDate = (
    SELECT MAX(effectiveDate)
    FROM ExchangeRates er2
    WHERE er1.fromCur = er2.fromCur
     AND er1.toCur = er2.toCur
)

hth
Phil
 
Old January 26th, 2005, 10:52 AM
Friend of Wrox
 
Join Date: Jun 2003
Posts: 1,212
Thanks: 0
Thanked 1 Time in 1 Post
Default

another way to do this without a correlated subquery (and possibly easier to understand) is like this:

SELECT er1.fromCur, er1.toCur, er1.rate, er1.effectiveDate
FROM ExchangeRates er1
INNER JOIN
(SELECT fromCur, toCur, MAX(effectiveDate) as effectiveDate
FROM ExchangeRates
GROUP BY fromCur, toCur) tmp
ON er1.fromCur=tmp.fromCur
   AND er1.toCur=tmp.toCur
   AND er1.effectiveDate=tmp.effectiveDate

correlated subqueries can be expensive, so check out the estimated execution plan to see which way is more efficient for your table.

rgds
Phil
 
Old January 26th, 2005, 01:00 PM
Registered User
 
Join Date: Jan 2005
Posts: 6
Thanks: 0
Thanked 0 Times in 0 Posts
Default

these work great - thanks for the replies.
kb






Similar Threads
Thread Thread Starter Forum Replies Last Post
How Run .sql Script file in MS SQL Server 2000? aarkaycee SQL Server 2000 5 October 12th, 2009 05:43 AM
creating ssis packagte for sql server to sql serer Laxmikant_it ASP.NET 3.5 Professionals 0 November 26th, 2008 12:23 AM
Converting from MS SQL 2005 to Sql Epress edition saif44 SQL Language 0 February 16th, 2007 04:17 PM
Failed to copy objects from SQL server to SQL Serv monfu SQL Server 2000 4 December 4th, 2005 05:54 PM
Move SQL DB from one sql to another sql server Israr SQL Server 2000 3 January 24th, 2005 02:13 PM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.