MS SQL Development
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me
Go Back   Dev Shed ForumsDatabasesMS SQL Development

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
  #1  
Old April 27th, 2004, 09:01 PM
jen_71 jen_71 is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Apr 2004
Posts: 4 jen_71 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
Selecting MAX(date) from multiple rows

hi, I have an sql statement returning the rows of a table based on a date. The trouble is, it is returning duplicated rows (transactionID's with multiple dates) when I need it to return only the row with the Maximum date for each transactionID.
This is the code I am using:

SELECT *
FROM tblExchRates RIGHT JOIN tblTransactions ON tblExchRates.currency=tblTransactions.currency
WHERE (((tblExchRates.Date)<=tblTransactions.Date) And ((tblExchRates.currency)=tblTransactions.currency));

Thanks all.
Jen

Reply With Quote
  #2  
Old April 27th, 2004, 09:47 PM
Username=NULL Username=NULL is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Apr 2004
Location: TX
Posts: 249 Username=NULL User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 1 Day 7 h 6 m 42 sec
Reputation Power: 5
Send a message via Yahoo to Username=NULL
First off, I don't totally get this...I'm not saying it's wrong, but I think I'm confused...maybe you or someone can clarify if there is value in the following...take a look @ your outer join and then your Where clause...
Code:
SELECT *
FROM   tblExchRates
RIGHT  JOIN tblTransactions
       ON tblExchRates.currency=tblTransactions.currency
WHERE  (
          ((tblExchRates.Date)<=tblTransactions.Date)
             AND
          ((tblExchRates.currency)=tblTransactions.currency)
        );

...if I understand correctly, you're basically making the outer join obselete. You want every record from the tblTransactions table, whether there's a match on both table's currency field or not, but then in your WHERE clause you're limiting the return rows where you want both currecy fields to match...
(tblExchRates.currency = tblTransactions.currency)
...you're basically taking an outer join and making it an inner join through more logic...just re-write your query to use an inner join instead of an outer join...@least that's what I would do. If there's other value in having that outer join for some reason, then keep it as is I guess.

Now...the max Date. I don't totally understand...could you post the 2 tables that you're joining...the fields and how they're related, then what fields you want in the output? I took it that you want the Transaction ID and the max(transactionDate) returned...for that you don't need a join @ all...I'm a little confused. We got your solution though, just need some clarifcation.

Last edited by Username=NULL : April 27th, 2004 at 10:09 PM.

Reply With Quote
  #3  
Old April 28th, 2004, 12:47 AM
jen_71 jen_71 is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Apr 2004
Posts: 4 jen_71 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
ok thanks for ur reply..
Let me go back..
The 2 tbles I am concerned with are: tblTransactions and tblExchangeRates

tblTransactions has:
transactID autoNumber
Date date of transaction
expCategory selected from a combo box on tblCategory
transCurrency selected from a combo box on tblLocation
transAmt cost of transaction
exchRate $amount from tblExchangeRate
AUD$ transAmt converted to AUD$ based on
exchRate


tblExchangeRate has:
exRateID autoNumber
exRateDate date of exchange rate
exAmount amount of exchange rate
exCurrency selected from tblLocation
(ie Rupee, Baht, US$...etc)

I want to create a form to add transactions (based on tblTransactions) and have the "exchRate" automatically entered based on the transaction Date.

The tblExchangeRate can be missing dates for a particular currency, so the transaction table needs to select the latest date in the tblExchangeRate to retrieve "exAmount" where the tlbTransaction.currency = tblExchangeRate.currency.

whew, ok, I hope I have explained that well enough.

Thanks again.

Reply With Quote
Reply

Viewing: Dev Shed ForumsDatabasesMS SQL Development > Selecting MAX(date) from multiple rows


Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump


Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 





© 2003-2008 by Developer Shed. All rights reserved. DS Cluster 1 hosted by Hostway
Stay green...Green IT