Please start any new threads on our new
site at https://forums.sqlteam.com. We've got lots of great SQL Server
experts to answer whatever question you can come up with.
Author |
Topic |
rwaldron
Posting Yak Master
131 Posts |
Posted - 2006-09-11 : 08:03:30
|
Hi all,I am converting old MSAccess queries to SQL.I have a query that picks the latest exchange rate based on the most recent date..Previously in msaccess this was done using the LAST Function but From What I have read the LAST Function is no longer supported in SQL2000.. Is there another way for me to get back the exchange rates...My current data looks like this.Country ExChange_rate ExdateGBP .69 20050125GBP .66 20060105 IRE .78 20061109IRE .80 20060101So I need A Query that will return the most recent ie:GBP .66 20060105 IRE .78 20061109Please helpRay |
|
chiragkhabaria
Master Smack Fu Yak Hacker
1907 Posts |
Posted - 2006-09-11 : 08:08:43
|
Select * From yourTable t1 where ExDate = (Select Max(ExDate) From yourTable t2 where t1.Country = t2.Country)Chirag |
 |
|
rwaldron
Posting Yak Master
131 Posts |
Posted - 2006-09-11 : 09:35:07
|
Hiya ,Thx for the reply...I am a bit confused though..I only have one table CCURRA...Not t1 & t2 ??I also use a Distributed Query.Using your example I have tried the following live codeSELECT *FROM OPENQUERY (MOVEX_EXTRANET, 'select * FROM MVXADTASWC.CCURRAwhere cucutd = (Select Max(cucutd) FROM MVXADTASWC.CCURRA where cucucd = cucucd) ')When I run the query I only get one record resultwhere cucucd = 20050930 which is more than likely then newest datebut I Should get a row like this returned for each Country ?Any more help please.Ray.. |
 |
|
SwePeso
Patron Saint of Lost Yaks
30421 Posts |
Posted - 2006-09-11 : 09:40:23
|
Try this. An advice though, if you are having trouble with aliases, take care when writing queries.Select * From MVXADTASWC.CCURRA t1 where t1.cucutd = (Select Max(t2.cucutd) From MVXADTASWC.CCURRA t2 where t2.cucucd = t1.cucucd) Peter LarssonHelsingborg, Sweden |
 |
|
X002548
Not Just a Number
15586 Posts |
Posted - 2006-09-11 : 10:51:40
|
I wonder if you use Pofile to trace what an ODBC call to SQL server would show for FIRST() and LAST()...since the order of data in a database has no meaning, it would have to convert it to something like MIN or MAX....unless it converts it to something LIKE SELECT @x = Col, which for a multi-row result would return an single arbitrary column...Brett8-)Hint: Want your questions answered fast? Follow the direction in this linkhttp://weblogs.sqlteam.com/brettk/archive/2005/05/25/5276.aspxAdd yourself!http://www.frappr.com/sqlteam |
 |
|
rwaldron
Posting Yak Master
131 Posts |
Posted - 2006-09-11 : 11:42:46
|
Thx for all your help so far guys..I nearly have it..Using the advice previously I have the following code to bring back the most recent Records per country.SELECT *FROM OPENQUERY (MOVEX_EXTRANET, 'Select * From MVXADTASWC.CCURRA t1 where t1.cucutd = (Select Max(t2.cucutd) From MVXADTASWC.CCURRA t2 where t2.cucucd = t1.cucucd) AND CUCONO = 400 AND CUCRTP = 1 ' )I get 17 records returned ( but access has 18 returned.)After checking I can see that the EURO value is left out of the final result table..I can see why this is happening but need a way around it....This is happening because of the data that relates to the Euro.In my DB I have the following records where country is EurCUCONO CUCUCD CUCRTP CUCUTD CUARAT400 EUR 1 20010101 1402 EUR 1 20010101 1500 EUR 1 20050101 1.45If I use the max code as advised then CUCONO 500 is seen as the most recent exchange rate as its date is in 2005 but I wish to filter on CUCONO=400. If I add CUCONO=400 before I use the max code I STILL do not get a Euro value back ?? |
 |
|
X002548
Not Just a Number
15586 Posts |
Posted - 2006-09-11 : 14:44:51
|
I did the Profiler thing and watch Access execute...I did both Max and Fisrt...first does nothing and returns all the rows...access must take the first one in the result setSQL:BatchCompleted SELECT "OrderID" FROM "dbo"."Orders" Microsoft® Access sa 0 21 0 30 2248 58 2006-09-11 14:39:43.890 SQL:BatchStarting SELECT MAX("OrderID" ) FROM "dbo"."Orders" Microsoft® Access sa 2248 58 2006-09-11 14:40:28.967 Brett8-)Hint: Want your questions answered fast? Follow the direction in this linkhttp://weblogs.sqlteam.com/brettk/archive/2005/05/25/5276.aspxAdd yourself!http://www.frappr.com/sqlteam |
 |
|
SwePeso
Patron Saint of Lost Yaks
30421 Posts |
Posted - 2006-09-11 : 15:02:39
|
One Euro row missing? Most probably because you use Euro for at least two countries.Peter LarssonHelsingborg, Sweden |
 |
|
|
|
|