473,406 Members | 2,956 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,406 software developers and data experts.

Problem with date based query

I have two tables - Transactions and Rates.

Table: Txns
Refce TxnDate Curr Amount
10 21/03/2006 USD 10.87
11 10/04/2006 USD 20.00

Table: Rates
Curr RateDate Rate
EUR 01/01/2006 1.41
USD 01/01/2006 1.80
USD 01/03/2006 1.81
USD 01/04/2006 1.82
USD 01/05/2006 1.83

I want to link these tables in a Query so that I can value the
transactions using the currency rate in force at the transaction date.
In this example, for Refce=10, the rate should be 1.81; for Refce=11,
it should be 1.82.

If only a forward dated rate is found, it should be ignored.

All dates are ddmmyyyy

Can this be done in a *single* Query?
Can anyone help with the SQL please?

Thanks
Dave

Jun 1 '06 #1
4 1302
This:

SELECT txns.Curr, txns.Refce, Format([txns].[txndate],"d mmm yyyy") AS
TxnDate, Format((Select Max(rM.ratedate) From rates as rM Where rM.ratedate
< txns.TxnDate And rM.curr=txns.Curr),"d mmm yyyy") AS ApplicableRateDate,
(Select rR.Rate From rates as rR Where rR.Curr = txns.Curr and rR.RateDate =
(Select Max(rM.ratedate) From rates as rM Where rM.ratedate < txns.TxnDate
And rM.curr=txns.Curr)) AS ApplicableRate
FROM txns;
returned this:

Curr Refce TxnDate ApplicableRateDate ApplicableRate
USD 10 21 Mar 2006 1 Mar 2006 1.81
USD 11 10 Apr 2006 1 Apr 2006 1.82

the sql could should be trimmed down to this:

SELECT txns.Curr, txns.Refce, txns.Amount, txns.TxnDate AS TxnDate, (Select
rR.Rate From rates as rR Where rR.Curr = txns.Curr and rR.RateDate = (Select
Max(rM.ratedate) From rates as rM Where rM.ratedate < txns.TxnDate And
rM.curr=txns.Curr)) AS ApplicableRate
FROM txns;

I had some formatting in the first version to show dates unequivocally as d
mmm yyyy so the month would be spelled out.

In case you're interested, there are two correlated subqueries used. The
lowest level is the (Select max...) that finds the latest date earlier than
the transaction date for the currency type. That one is fed up one level to
the (Select rR.Rate...) that finds the rate that matches the date returned
by the lower level one.

In the first version I accidentally omitted the Amount column. I just
noticed.

The other thing of note perhaps is the use of aliases. In both subqueries I
used aliases because it saves on typing when specifying, for instance, which
Curr field is being referenced. use of aliases also helps in that you can
use the alias to distinguish for yourself the use of the table where, as in
this example, the same table is used in two contexts. In the lower level
subquery I select from rates in order only to find the maximum date less
than my transaction date. Hence rM, M for Max. In the upper subquery I
refer to the rates table in order to get the rate, hence rR. R for rate.

Fun stuff.
Jun 1 '06 #2
Excellent, thanks very much for your time and effort.

Dave

w_a_n_n_a_l_l_-@-_s_b_c_g_l_o_b_a_l._n_e_t wrote:
This:

SELECT txns.Curr, txns.Refce, Format([txns].[txndate],"d mmm yyyy") AS
TxnDate, Format((Select Max(rM.ratedate) From rates as rM Where rM.ratedate
< txns.TxnDate And rM.curr=txns.Curr),"d mmm yyyy") AS ApplicableRateDate,
(Select rR.Rate From rates as rR Where rR.Curr = txns.Curr and rR.RateDate =
(Select Max(rM.ratedate) From rates as rM Where rM.ratedate < txns.TxnDate
And rM.curr=txns.Curr)) AS ApplicableRate
FROM txns;
returned this:

Curr Refce TxnDate ApplicableRateDate ApplicableRate
USD 10 21 Mar 2006 1 Mar 2006 1.81
USD 11 10 Apr 2006 1 Apr 2006 1.82

the sql could should be trimmed down to this:

SELECT txns.Curr, txns.Refce, txns.Amount, txns.TxnDate AS TxnDate, (Select
rR.Rate From rates as rR Where rR.Curr = txns.Curr and rR.RateDate = (Select
Max(rM.ratedate) From rates as rM Where rM.ratedate < txns.TxnDate And
rM.curr=txns.Curr)) AS ApplicableRate
FROM txns;

I had some formatting in the first version to show dates unequivocally as d
mmm yyyy so the month would be spelled out.

In case you're interested, there are two correlated subqueries used. The
lowest level is the (Select max...) that finds the latest date earlier than
the transaction date for the currency type. That one is fed up one level to
the (Select rR.Rate...) that finds the rate that matches the date returned
by the lower level one.

In the first version I accidentally omitted the Amount column. I just
noticed.

The other thing of note perhaps is the use of aliases. In both subqueries I
used aliases because it saves on typing when specifying, for instance, which
Curr field is being referenced. use of aliases also helps in that you can
use the alias to distinguish for yourself the use of the table where, as in
this example, the same table is used in two contexts. In the lower level
subquery I select from rates in order only to find the maximum date less
than my transaction date. Hence rM, M for Max. In the upper subquery I
refer to the rates table in order to get the rate, hence rR. R for rate.

Fun stuff.


Jun 1 '06 #3
Excellent, thanks very much for your time and effort.

Dave

w_a_n_n_a_l_l_-@-_s_b_c_g_l_o_b_a_l._n_e_t wrote:
This:

SELECT txns.Curr, txns.Refce, Format([txns].[txndate],"d mmm yyyy") AS
TxnDate, Format((Select Max(rM.ratedate) From rates as rM Where rM.ratedate
< txns.TxnDate And rM.curr=txns.Curr),"d mmm yyyy") AS ApplicableRateDate,
(Select rR.Rate From rates as rR Where rR.Curr = txns.Curr and rR.RateDate =
(Select Max(rM.ratedate) From rates as rM Where rM.ratedate < txns.TxnDate
And rM.curr=txns.Curr)) AS ApplicableRate
FROM txns;
returned this:

Curr Refce TxnDate ApplicableRateDate ApplicableRate
USD 10 21 Mar 2006 1 Mar 2006 1.81
USD 11 10 Apr 2006 1 Apr 2006 1.82

the sql could should be trimmed down to this:

SELECT txns.Curr, txns.Refce, txns.Amount, txns.TxnDate AS TxnDate, (Select
rR.Rate From rates as rR Where rR.Curr = txns.Curr and rR.RateDate = (Select
Max(rM.ratedate) From rates as rM Where rM.ratedate < txns.TxnDate And
rM.curr=txns.Curr)) AS ApplicableRate
FROM txns;

I had some formatting in the first version to show dates unequivocally as d
mmm yyyy so the month would be spelled out.

In case you're interested, there are two correlated subqueries used. The
lowest level is the (Select max...) that finds the latest date earlier than
the transaction date for the currency type. That one is fed up one level to
the (Select rR.Rate...) that finds the rate that matches the date returned
by the lower level one.

In the first version I accidentally omitted the Amount column. I just
noticed.

The other thing of note perhaps is the use of aliases. In both subqueries I
used aliases because it saves on typing when specifying, for instance, which
Curr field is being referenced. use of aliases also helps in that you can
use the alias to distinguish for yourself the use of the table where, as in
this example, the same table is used in two contexts. In the lower level
subquery I select from rates in order only to find the maximum date less
than my transaction date. Hence rM, M for Max. In the upper subquery I
refer to the rates table in order to get the rate, hence rR. R for rate.

Fun stuff.


Jun 1 '06 #4
Excellent, thanks very much for your time and effort.

Dave

w_a_n_n_a_l_l_-@-_s_b_c_g_l_o_b_a_l._n_e_t wrote:
This:

SELECT txns.Curr, txns.Refce, Format([txns].[txndate],"d mmm yyyy") AS
TxnDate, Format((Select Max(rM.ratedate) From rates as rM Where rM.ratedate
< txns.TxnDate And rM.curr=txns.Curr),"d mmm yyyy") AS ApplicableRateDate,
(Select rR.Rate From rates as rR Where rR.Curr = txns.Curr and rR.RateDate =
(Select Max(rM.ratedate) From rates as rM Where rM.ratedate < txns.TxnDate
And rM.curr=txns.Curr)) AS ApplicableRate
FROM txns;
returned this:

Curr Refce TxnDate ApplicableRateDate ApplicableRate
USD 10 21 Mar 2006 1 Mar 2006 1.81
USD 11 10 Apr 2006 1 Apr 2006 1.82

the sql could should be trimmed down to this:

SELECT txns.Curr, txns.Refce, txns.Amount, txns.TxnDate AS TxnDate, (Select
rR.Rate From rates as rR Where rR.Curr = txns.Curr and rR.RateDate = (Select
Max(rM.ratedate) From rates as rM Where rM.ratedate < txns.TxnDate And
rM.curr=txns.Curr)) AS ApplicableRate
FROM txns;

I had some formatting in the first version to show dates unequivocally as d
mmm yyyy so the month would be spelled out.

In case you're interested, there are two correlated subqueries used. The
lowest level is the (Select max...) that finds the latest date earlier than
the transaction date for the currency type. That one is fed up one level to
the (Select rR.Rate...) that finds the rate that matches the date returned
by the lower level one.

In the first version I accidentally omitted the Amount column. I just
noticed.

The other thing of note perhaps is the use of aliases. In both subqueries I
used aliases because it saves on typing when specifying, for instance, which
Curr field is being referenced. use of aliases also helps in that you can
use the alias to distinguish for yourself the use of the table where, as in
this example, the same table is used in two contexts. In the lower level
subquery I select from rates in order only to find the maximum date less
than my transaction date. Hence rM, M for Max. In the upper subquery I
refer to the rates table in order to get the rate, hence rR. R for rate.

Fun stuff.


Jun 1 '06 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Raghu | last post by:
Hello... I am running into a problem while running a query..can some1 help.. this is the query : ************** SELECT * from Table S where S.dtDate1 BETWEEN...
3
by: rrh | last post by:
I am trying to update a field in one table with data from another table. The problem I'm running into is I need to base the update on a range of data in the 2nd table. Table 1 has: date field...
6
by: Mark Reed | last post by:
Hi Guru's, I have created a database to monitor hours I have worked as our payroll department are so crap. I work nights most of the time but occasionally I have to work on days. Between the hours...
3
by: StBond | last post by:
Hi everyone, I am new to Access and Visual Basic so things my be getting across a bit cloudy. I only started using VB for one week. I am having a little problem with the database that I am...
14
by: marcokrechting | last post by:
Hi All, I have a rather complex math problem concerning dates. I will try to explain my problem. I have a table with the fields SUBJECT (text), DUE DATE (date) and CHECKED (yes/no). In this...
1
by: ET | last post by:
Please help with the query: There are two tables, A and B. A table has information about cell phones, like cell number, sim number, model, manufacturer etc... B table has user related...
2
by: DeanL | last post by:
Hi all, I have a problem that I'm a little stumped by and need some help if possible. I need to generate a report in Access 97 from 2 tables (easy so far) but it requires a calculated date...
5
by: ShadesOfGrey | last post by:
Hi, new to the group, not to Access...but it's been awhile since I've done much with it. I am creating a database that tracks student and instructor attendance. I have two tables, one for student...
2
by: dympna | last post by:
Hi can anyone suggest a fix for this... as I am a novice in access. I have created a training table with the following fields Employee Name - joe Training Received - Fork lift Date Received...
4
by: pokerboy801 | last post by:
OK, I will try to explain this as clearly and as concise as possible. I am using Access, which has three MS Excel Linked tables, to store call center metrics for reps. My Excel workbook has three...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.