473,466 Members | 1,508 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

Problem with sub queries in Access

I am having a great deal of trouble with subqueries in Access.

I am trying to write a Payroll application, I have the following tables

PayRecords
+----+------------+--------+----------+
| Id | Date | Pay | Employee |
+----+------------+--------+----------+
| 0 | 19/04/2001 | 123.45 | 1 |
| 1 | 19/05/2001 | 123.45 | 1 |
| 2 | 19/06/2001 | 123.45 | 1 |
| 3 | 19/07/2001 | 123.45 | 1 |
| 4 | 19/08/2001 | 123.45 | 1 |
| 5 | 19/04/2001 | 97.68 | 2 |
| 6 | 19/05/2001 | 97.68 | 2 |
| 7 | 19/06/2001 | 97.68 | 2 |
| 8 | 19/07/2001 | 97.68 | 2 |
| 9 | 19/08/2001 | 97.68 | 2 |
+----+------------+--------+----------+

Employees
+----+------------+------------+
| Id | Surname | First name |
+----+------------+------------+
| 1 | Bloggs | Joe |
| 2 | Smith | Bill |
+----+------------+------------+
TaxCodes
+----+------------+------------+-------+
| Id | Date | Employee | Code |
+----+------------+------------+-------+
| 1 | 06/04/2001 | 1 | 99L |
| 2 | 06/06/2001 | 1 | 105L |
| 3 | 06/04/2001 | 2 | 150L |
| 4 | 06/07/2001 | 2 | 167L |
+----+------------+------------+-------+

The tax code table shows on what date a particular tax code applies for
an employee.

Once a tax code applies then earlier tax codes for that employee do not
apply.

So in the example above

Joe Bloggs has tax code 99L from 06/04/2001 until 06/06/2001 when he
then has tax code 105L

Similarly Bill Smith has tax code 150L from 06/04/2001 until 06/07/2001
when tax code 167L applies.

When working out the tax owed by an employee on each pay day I need to
determine which tax code is applicable.

This is where my problems lie.

What I think I want to query for is the tax code with the highest date
that is less than or equal to the PayRecords.Date field and that the
tax code and pay record employee fields are equal :-

SELECT PayRecords.Employee, PayRecords.Date,
PayRecords.PayTaxCodes.Code
FROM PayRecords, TaxCodes

WHERE (Max(TaxCodes.Date) <= PayRecords.Date) AND (PayRecords.Employee
= TaxCodes.Employee);

This gives me an error because you cannot have an aggregate function in
a WHERE clause.

After some research on the web I tried the following

SELECT PayRecords.Employee, PayRecords.Date, PayRecords.Pay,
TaxCodes.Code, TaxCodes.Date
FROM PayRecords, TaxCodes

WHERE PayRecords.Date >= (select Max(TaxCodes.Date) As TaxCodesDate
FROM TaxCodes WHERE (PayRecords.Employee = TaxCodes.Employee))

ORDER BY PayRecords.Employee, PayRecords.Date;
This query runs but it gives multple instances of the same payrecord
with different tax codes, some of which have dates later than the
payrecord's date. It seems to be ignoring the >= test

Does anybody know what I am doing wrong ?

I would be most grateful for any help people can offer.


regards
Darran

Nov 13 '05 #1
1 1722
Hi Darran,

You've posted a lot of detail, which has made it a lot easier to debug what
you're trying to do - thanks!

As a first up comment, you should rename the fields you've called Date to
more specific things like PayDate and TaxDate - "Date" has a particular
meaning in VBA in particular, which can cuase you difficulties later on
down the track.

OK, the reason you're getting multiple records is because your WHERE clause
only joins PayRecords.Date with the returned values, irrespective of what
Employee they apply to - I don't know of any way to use the subquery to
check on more than one field, so my recommendation is to create some new
queries that you can use to get what you need. This may be covered in
theory somewhere else, but maybe it will be helpful

Firstly, to get all of the pay records with all of the tax codes that come
into effect after the pay period:
PayAndLaterTax
SELECT PayRecords.Id, PayRecords.Employee, PayRecords.PayDate,
PayRecords.Pay, TaxCodes.TaxDate, TaxCodes.Code
FROM PayRecords INNER JOIN TaxCodes ON PayRecords.Employee =
TaxCodes.Employee
WHERE (((TaxCodes.TaxDate)>[PayRecords].[PayDate]));

We can then join this to the original TaxCode table to figure out what the
ones less than the Pay Date are:
PayAndEarlierTax
SELECT PayAndLaterTax.Id, PayAndLaterTax.Employee, PayAndLaterTax.PayDate,
PayAndLaterTax.Pay, TaxCodes.TaxDate, TaxCodes.Code
FROM PayAndLaterTax INNER JOIN TaxCodes ON PayAndLaterTax.Employee =
TaxCodes.Employee
WHERE (((TaxCodes.TaxDate)<[PayAndLaterTax].[TaxDate]));

In your example, this would be enough: however, this query returns ALL of
the Tax Codes that start before the date you want - you actually want the
biggest one of these dates, for which you need another two queries:
PayAndEarlierTaxMax
SELECT PayAndEarlierTax.Employee, Max(PayAndEarlierTax.TaxDate) AS
MaxOfTaxDate
FROM PayAndEarlierTax
GROUP BY PayAndEarlierTax.Employee;

And then use this Date to join back to your other query:
PayAndApplicableTax
SELECT PayAndEarlierTax.*
FROM PayAndEarlierTax INNER JOIN PayAndEarlierTaxMax ON
(PayAndEarlierTax.TaxDate = PayAndEarlierTaxMax.MaxOfTaxDate) AND
(PayAndEarlierTax.Employee = PayAndEarlierTaxMax.Employee);

I may have gotten this last query wrong - I'm not 100% sure that joining on
the TaxDates alone is enough, but give it a go on a bigger sample set and
let me know.

Cheers,
David...

--
Message posted via http://www.accessmonster.com
Nov 13 '05 #2

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

Similar topics

10
by: JMorrell | last post by:
First post to this community so am not sure if this is the correct place. Here goes. I have a MS Access db that keeps track of employees sick and annual leave balances. In it, I have a report,...
4
by: John Smith | last post by:
Isn't life a bitch! You know what you want but you don't know how to get it. I have produced 12 queries that calculate a payment profile over 12 months. For a number of the records (ie with...
8
by: Alfonso Esteban Gonzalez Sencion | last post by:
I am trying to use Access as a front end for extracting information from an Oracle database. I started using linked tables but I am getting a very curious behaviour. When I consult the linked...
1
by: christopher_mouse | last post by:
I have a split Access 97 database that I've been asked to secure. The data is sensitive and users include programmers who are routinely accessing the data directly, so I plan to remove all...
10
by: Saso Zagoranski | last post by:
hi, this is not actually a C# problem but since this is the only newsgroup I follow I decided to post my question here (please tell me where to post this next time if you think this post...
0
by: kkrizl | last post by:
I've tried to research this problem, and I haven't been able to find any references to it. Probably because I shouldn't be doing it, but it was working, and now it's not. I'm trying to develop a...
1
by: kkrizl | last post by:
I've tried to research this problem, and I haven't been able to find any references to it. Probably because I shouldn't be doing it, but it was working, and now it's not. I'm trying to develop a...
1
by: eblackmo | last post by:
I have a test network consisting of four servers running windows 2003 server R2 SP2. I have set up a domain which functioned correctly for about a day and a half until the other servers decided they...
3
prn
by: prn | last post by:
Hi folks, I'm trying to create letters based on an Access database. I'm using Access 2003 and Word 2003. Most of the data tables are actually on a SQL server and Access is the front end for them....
13
by: harshakusam | last post by:
Hi All, MDB file with 110 passthru queries Run each query and save it in excel.. I had already written VBcode.... my code works fine for normal queries.. but all my queries using case, substr,...
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
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
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
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...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...

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.