Database Management
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me

The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.

Go Back   Dev Shed ForumsDatabasesDatabase Management

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 December 31st, 2011, 07:50 AM
YoungL YoungL is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: May 2011
Posts: 11 YoungL User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 2 h 15 m 42 sec
Reputation Power: 0
Thumbs up Query to take a date from one table and retrieve the nearest date from another table.

I have two tables which are:

Transactions
------------

Code:
date       | Value 
-------------------------
01/01/2010 | 14.95        
02/01/2010 | 15.95        
05/01/2010 | 15.95        
10/01/2010 | 14.95       


I then have another table

ExchangeRates
---------------
Code:
date        | Rate
--------------------------
03/01/2010  | 0.609563
06/01/2010  | 0.610535
10/01/2010  | 0.650213


I want the query to select all the values in the transaction table and find the nearest higher date in the exchange table and give me the rate. So my results would be:

Query Results
-------------
Code:
date       | value | xdate       |  Rate
--------------------------------------------
01/01/2010 | 14.95 | 03/01/2010  | 0.609563  
02/01/2010 | 15.95 | 03/01/2010  | 0.609563  
05/01/2010 | 15.95 | 06/01/2010  | 0.610535 
10/01/2010 | 14.95 | 10/01/2010  | 0.650213


I think it is probably pretty easy to do but I just cannot get my head around this one. I appreciate the help

Reply With Quote
  #2  
Old December 31st, 2011, 07:56 PM
r937's Avatar
r937 r937 is offline
SQL Consultant
Click here for more information.
 
Join Date: Feb 2003
Location: Toronto Canada
Posts: 26,380 r937 User rank is General 47th Grade (Above 100000 Reputation Level)r937 User rank is General 47th Grade (Above 100000 Reputation Level)r937 User rank is General 47th Grade (Above 100000 Reputation Level)r937 User rank is General 47th Grade (Above 100000 Reputation Level)r937 User rank is General 47th Grade (Above 100000 Reputation Level)r937 User rank is General 47th Grade (Above 100000 Reputation Level)r937 User rank is General 47th Grade (Above 100000 Reputation Level)r937 User rank is General 47th Grade (Above 100000 Reputation Level)r937 User rank is General 47th Grade (Above 100000 Reputation Level)r937 User rank is General 47th Grade (Above 100000 Reputation Level)r937 User rank is General 47th Grade (Above 100000 Reputation Level)r937 User rank is General 47th Grade (Above 100000 Reputation Level)r937 User rank is General 47th Grade (Above 100000 Reputation Level)r937 User rank is General 47th Grade (Above 100000 Reputation Level)r937 User rank is General 47th Grade (Above 100000 Reputation Level)r937 User rank is General 47th Grade (Above 100000 Reputation Level) 
Time spent in forums: 3 Months 1 Week 2 Days 7 h 50 m 34 sec
Reputation Power: 4140
Code:
SELECT t.date
     , t.value
     , e.date as xdate
     , e.rate
  FROM transactions AS t
INNER
  JOIN exchangerates AS e
    ON e.date =
       ( SELECT MIN(date)
           FROM exchangerates
          WHERE date >= t.date )
you actually said "greater than" but by inspecting your desired results, it's clear you meant "equal or greater"

__________________
r937.com | rudy.ca
please visit Simply SQL and buy my book

Reply With Quote
  #3  
Old January 1st, 2012, 08:19 AM
YoungL YoungL is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: May 2011
Posts: 11 YoungL User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 2 h 15 m 42 sec
Reputation Power: 0
Yes I did mean greater than or equal to. I should of made that clearer.

I am getting a syntax error on

Code:
    ON e.date =
       ( SELECT MIN(date)
           FROM exchangerates
          WHERE date >= t.date )


Can't seem to fix the error. Any ideas?

Thanks for your help.


Quote:
Originally Posted by r937
Code:
SELECT t.date
     , t.value
     , e.date as xdate
     , e.rate
  FROM transactions AS t
INNER
  JOIN exchangerates AS e
    ON e.date =
       ( SELECT MIN(date)
           FROM exchangerates
          WHERE date >= t.date )
you actually said "greater than" but by inspecting your desired results, it's clear you meant "equal or greater"


Reply With Quote
  #4  
Old January 1st, 2012, 09:18 AM
r937's Avatar
r937 r937 is offline
SQL Consultant
Click here for more information.
 
Join Date: Feb 2003
Location: Toronto Canada
Posts: 26,380 r937 User rank is General 47th Grade (Above 100000 Reputation Level)r937 User rank is General 47th Grade (Above 100000 Reputation Level)r937 User rank is General 47th Grade (Above 100000 Reputation Level)r937 User rank is General 47th Grade (Above 100000 Reputation Level)r937 User rank is General 47th Grade (Above 100000 Reputation Level)r937 User rank is General 47th Grade (Above 100000 Reputation Level)r937 User rank is General 47th Grade (Above 100000 Reputation Level)r937 User rank is General 47th Grade (Above 100000 Reputation Level)r937 User rank is General 47th Grade (Above 100000 Reputation Level)r937 User rank is General 47th Grade (Above 100000 Reputation Level)r937 User rank is General 47th Grade (Above 100000 Reputation Level)r937 User rank is General 47th Grade (Above 100000 Reputation Level)r937 User rank is General 47th Grade (Above 100000 Reputation Level)r937 User rank is General 47th Grade (Above 100000 Reputation Level)r937 User rank is General 47th Grade (Above 100000 Reputation Level)r937 User rank is General 47th Grade (Above 100000 Reputation Level) 
Time spent in forums: 3 Months 1 Week 2 Days 7 h 50 m 34 sec
Reputation Power: 4140
my new year's resolution: be more polite

please, won't you tell me the error message?

Reply With Quote
  #5  
Old January 1st, 2012, 09:28 AM
YoungL YoungL is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: May 2011
Posts: 11 YoungL User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 2 h 15 m 42 sec
Reputation Power: 0
Syntax error in query expression 'e.date = ( SELECT MIN(date) FROM exchangerates WHERE date >= t.date'.

that is the error message I am getting.

I should of also been more clear this is a Ms Access database, I am not sure if that makes any difference.

Reply With Quote
  #6  
Old January 1st, 2012, 01:32 PM
r937's Avatar
r937 r937 is offline
SQL Consultant
Click here for more information.
 
Join Date: Feb 2003
Location: Toronto Canada
Posts: 26,380 r937 User rank is General 47th Grade (Above 100000 Reputation Level)r937 User rank is General 47th Grade (Above 100000 Reputation Level)r937 User rank is General 47th Grade (Above 100000 Reputation Level)r937 User rank is General 47th Grade (Above 100000 Reputation Level)r937 User rank is General 47th Grade (Above 100000 Reputation Level)r937 User rank is General 47th Grade (Above 100000 Reputation Level)r937 User rank is General 47th Grade (Above 100000 Reputation Level)r937 User rank is General 47th Grade (Above 100000 Reputation Level)r937 User rank is General 47th Grade (Above 100000 Reputation Level)r937 User rank is General 47th Grade (Above 100000 Reputation Level)r937 User rank is General 47th Grade (Above 100000 Reputation Level)r937 User rank is General 47th Grade (Above 100000 Reputation Level)r937 User rank is General 47th Grade (Above 100000 Reputation Level)r937 User rank is General 47th Grade (Above 100000 Reputation Level)r937 User rank is General 47th Grade (Above 100000 Reputation Level)r937 User rank is General 47th Grade (Above 100000 Reputation Level) 
Time spent in forums: 3 Months 1 Week 2 Days 7 h 50 m 34 sec
Reputation Power: 4140
Quote:
Originally Posted by YoungL
... this is a Ms Access database, I am not sure if that makes any difference.
it often does

change this --

e.date = ( SELECT MIN(date) FROM exchangerates WHERE date >= t.date )

to this --

e.date = ( SELECT MIN(date) FROM exchangerates WHERE exchangerates.date >= t.date )

Reply With Quote
  #7  
Old January 1st, 2012, 05:17 PM
YoungL YoungL is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: May 2011
Posts: 11 YoungL User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 2 h 15 m 42 sec
Reputation Power: 0
No luck with that. Still getting the same error as above. Sorry for being a pain lol.

Reply With Quote
  #8  
Old January 1st, 2012, 05:51 PM
r937's Avatar
r937 r937 is offline
SQL Consultant
Click here for more information.
 
Join Date: Feb 2003
Location: Toronto Canada
Posts: 26,380 r937 User rank is General 47th Grade (Above 100000 Reputation Level)r937 User rank is General 47th Grade (Above 100000 Reputation Level)r937 User rank is General 47th Grade (Above 100000 Reputation Level)r937 User rank is General 47th Grade (Above 100000 Reputation Level)r937 User rank is General 47th Grade (Above 100000 Reputation Level)r937 User rank is General 47th Grade (Above 100000 Reputation Level)r937 User rank is General 47th Grade (Above 100000 Reputation Level)r937 User rank is General 47th Grade (Above 100000 Reputation Level)r937 User rank is General 47th Grade (Above 100000 Reputation Level)r937 User rank is General 47th Grade (Above 100000 Reputation Level)r937 User rank is General 47th Grade (Above 100000 Reputation Level)r937 User rank is General 47th Grade (Above 100000 Reputation Level)r937 User rank is General 47th Grade (Above 100000 Reputation Level)r937 User rank is General 47th Grade (Above 100000 Reputation Level)r937 User rank is General 47th Grade (Above 100000 Reputation Level)r937 User rank is General 47th Grade (Above 100000 Reputation Level) 
Time spent in forums: 3 Months 1 Week 2 Days 7 h 50 m 34 sec
Reputation Power: 4140
Quote:
Originally Posted by YoungL
No luck with that. Still getting the same error as above. Sorry for being a pain lol.
it's not you that's the pain, it's flippin msaccess

try writing [date] instead of date

Reply With Quote
  #9  
Old January 2nd, 2012, 03:30 PM
YoungL YoungL is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: May 2011
Posts: 11 YoungL User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 2 h 15 m 42 sec
Reputation Power: 0
It's still not liking it. It just doesn't seem to be liking having the subquery as part of the ON clause.

I can get the correct date from the second table using this query:

Code:
SELECT t.date
     , t.gross
     , ( SELECT MIN(exchangerates.date) AS xdate
         FROM exchangerates
         WHERE t.date <= exchangerates.date
        ) as xdate
FROM transactions AS t


But the trouble with this, is I don't know how to also get the rate. Every way I have tried doesn't seem to be working.

Reply With Quote
  #10  
Old January 3rd, 2012, 10:34 AM
r937's Avatar
r937 r937 is offline
SQL Consultant
Click here for more information.
 
Join Date: Feb 2003
Location: Toronto Canada
Posts: 26,380 r937 User rank is General 47th Grade (Above 100000 Reputation Level)r937 User rank is General 47th Grade (Above 100000 Reputation Level)r937 User rank is General 47th Grade (Above 100000 Reputation Level)r937 User rank is General 47th Grade (Above 100000 Reputation Level)r937 User rank is General 47th Grade (Above 100000 Reputation Level)r937 User rank is General 47th Grade (Above 100000 Reputation Level)r937 User rank is General 47th Grade (Above 100000 Reputation Level)r937 User rank is General 47th Grade (Above 100000 Reputation Level)r937 User rank is General 47th Grade (Above 100000 Reputation Level)r937 User rank is General 47th Grade (Above 100000 Reputation Level)r937 User rank is General 47th Grade (Above 100000 Reputation Level)r937 User rank is General 47th Grade (Above 100000 Reputation Level)r937 User rank is General 47th Grade (Above 100000 Reputation Level)r937 User rank is General 47th Grade (Above 100000 Reputation Level)r937 User rank is General 47th Grade (Above 100000 Reputation Level)r937 User rank is General 47th Grade (Above 100000 Reputation Level) 
Time spent in forums: 3 Months 1 Week 2 Days 7 h 50 m 34 sec
Reputation Power: 4140
try this --
Code:
SELECT y.date
     , y.gross
     , y.xdate
     , z.rate
  FROM ( SELECT t.date
              , t.gross
              , MIN(x.date) AS xdate
           FROM transactions AS t
         INNER
           JOIN exchangerates AS x
             ON x.date >= t.date
         GROUP
             BY t.date
              , t.gross ) AS y    
INNER
  JOIN exchangerates AS z               
    ON z.date = y.xdate

Reply With Quote
  #11  
Old January 3rd, 2012, 05:31 PM
YoungL YoungL is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: May 2011
Posts: 11 YoungL User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 2 h 15 m 42 sec
Reputation Power: 0
That is awesome, works a treat!!

Thank you so much for that, really appreciate it!

Reply With Quote
Reply

Viewing: Dev Shed ForumsDatabasesDatabase Management > Query to take a date from one table and retrieve the nearest date from another table.

Developer Shed Advertisers and Affiliates



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 | 
  
 


Powered by: vBulletin Version 3.0.5
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.

© 2003-2013 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap