473,378 Members | 1,347 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,378 software developers and data experts.

Query across three history tables

RLN
Re: Access 2000

I have three history tables.
Each table contains 3 years worth of data.
All three tables have a date field in them (and autonum field).
Each table has the potential to contain over 100K rows each.
A common request the user receives is a request to pull all data written
in a given month. I need to write some sort of union query across all
three tables that would pull data for, say, only the month of December
of all nine years.

I got a union query to work with two tables.
How do I do it with three or more tables?

Thanks for the assistance.
*** Sent via Devdex http://www.devdex.com ***
Don't just participate in USENET...get rewarded for it!
Nov 13 '05 #1
7 3630
You should be able top just add more UNION statements:

SELECT Fld1, Fld2 FROM tbl1
UNION SELECT Fld1, Fld2 FROM tbl2
UNION SELECT Fld1, Fld2 FROM tbl3;
etc.....
On 23 Jun 2004 21:10:36 GMT, RLN <no*******@devdex.com> wrote:
Re: Access 2000

I have three history tables.
Each table contains 3 years worth of data.
All three tables have a date field in them (and autonum field).
Each table has the potential to contain over 100K rows each.
A common request the user receives is a request to pull all data written
in a given month. I need to write some sort of union query across all
three tables that would pull data for, say, only the month of December
of all nine years.

I got a union query to work with two tables.
How do I do it with three or more tables?

Thanks for the assistance.
*** Sent via Devdex http://www.devdex.com ***
Don't just participate in USENET...get rewarded for it!


Nov 13 '05 #2
I got a union query to work with two tables.
How do I do it with three or more tables?

Thanks for the assistance.


SELECT field1, field2
FROM table1
UNION ALL
SELECT field1, field2
FROM table1...

will give you ALL the records, including duplicates (should there be any).

using a plain UNION will give you ONLY unique records.
Nov 13 '05 #3
RLN
Now I would like to take this union query a step further.
In each of these three test tables are two rows, each has a date in the
month of December, and TblTest2004 is the only table with 3 rows for the
month of March.
How can I pull just those 9 rows out of all three tables?
(6 total for 12/2003, 2004 & 2005, then also 3 for March in 2004 only?
The trick is that when this goes live, the user might not always know
what year the March data is located, but will only have the month.
I tried using the 'month' function as listed below, to no avail.
Suggestions?

SELECT ccdate, comments
FROM TblTest2003
UNION
SELECT ccdate, comments
FROM TblTest2004
UNION
SELECT ccdate, comments
FROM TblTest2005
where month(ccdate) =12 or month(ccdate = 3)

(I wasn't sure how to qualify the ccdate field from TblTest2003, ccdate
from TblTest2004, etc..etc.)

Suggestions are welcome.

*** Sent via Devdex http://www.devdex.com ***
Don't just participate in USENET...get rewarded for it!
Nov 13 '05 #4
> In each of these three test tables are two rows, each has a date in the
month of December, and TblTest2004 is the only table with 3 rows for the
month of March.
How can I pull just those 9 rows out of all three tables?
(6 total for 12/2003, 2004 & 2005, then also 3 for March in 2004 only?
The trick is that when this goes live, the user might not always know
what year the March data is located, but will only have the month.
I tried using the 'month' function as listed below, to no avail.
Suggestions?

SELECT ccdate, comments
FROM TblTest2003
UNION
SELECT ccdate, comments
FROM TblTest2004
UNION
SELECT ccdate, comments
FROM TblTest2005
where month(ccdate) =12 or month(ccdate = 3)

(I wasn't sure how to qualify the ccdate field from TblTest2003, ccdate
from TblTest2004, etc..etc.)


If you are filtering the whole dataset (the result of the unions),
build another query on this one which does the filtering. otherwise,
you need to include a WHERE clause for *each* select statement in your
union query.
Nov 13 '05 #5
SELECT ccdate, comments
FROM TblTest2003 where ccdate = #12/03/04#
UNION
SELECT ccdate, comments
FROM TblTest2004 where ccdate = #12/03/04#
UNION
SELECT ccdate, comments
FROM TblTest2005 where ccdate =#12/04/05#;

This union query runs, but using the Query Designer (SQL view)
I need to try two other flavors of this query:
1. check to see if the month is 03 (March) or 12 (December)
(I tried "month(ccdate)", which did not work.

2. set this query up to launch two dialogs: "a begin month" and an "end
month", then pulls all the history for that range of months in between.
(user inputs "4", then "9", the query would pull all data for April thru
September for all three years.

---------------
Thanks.

*** Sent via Devdex http://www.devdex.com ***
Don't just participate in USENET...get rewarded for it!
Nov 13 '05 #6
> This union query runs, but using the Query Designer (SQL view)
I need to try two other flavors of this query:
1. check to see if the month is 03 (March) or 12 (December)
(I tried "month(ccdate)", which did not work.
SELECT Table4.SomeDate, Month([SomeDate]) AS MonthNo
FROM Table4
WHERE (((Month([SomeDate])) In (3,12)));
2. set this query up to launch two dialogs: "a begin month" and an "end
month", then pulls all the history for that range of months in between.
(user inputs "4", then "9", the query would pull all data for April thru
September for all three years.

SELECT Table4.SomeDate, Month([SomeDate]) AS MonthNo
FROM Table4
WHERE (((Month([SomeDate])) Between [Enter a begin month:] And [Enter
an end month:]));
Nov 13 '05 #7

I am working to refine the selection process so that the date range gets
pulled across all three tables.
So far the tests are looking ok.
---------------
Thanks.

*** Sent via Devdex http://www.devdex.com ***
Don't just participate in USENET...get rewarded for it!
Nov 13 '05 #8

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

Similar topics

4
by: Orion | last post by:
Hi, This is kind of last minute, I have a day and a half left to figure this out. I'm working on a project using ms-sqlserver. We are creating a ticket sales system, as part of the system, I...
0
by: David | last post by:
This might be a report question but I think it is more a query issue than a report. I am a novice at bothAccess reporting and queries and have been struggling to build the following. (Note using...
1
by: NA | last post by:
Is it possible to have adjustable tables (not Access tables per se) but those in Ms Word or Excel based on nexted forms three deep? In other words, if I have a main form, Repair History, both the...
3
by: Ron Nolan | last post by:
I have a large application that contains lots and lots of financial history data. The history data is currently set up in a table called 'TblHist' that exists inside each of these three .mdb...
6
by: Martin Lacoste | last post by:
Ok, before I headbutt the computer... don't know why when I add criteria in a query, I get an 'invalid procedure call'. I also don't know why after searching the help in access, the various access...
3
by: mkjets | last post by:
I have worked for hours on trying to find a solution and have not figured it out. I am working in Access 2003. I need to create a query that takes values from 1 table and displays them in...
0
by: Nyh | last post by:
I tried to explain the best I can but it is very difficult to do. Here it goes. I have three history tables. Teacher, student and school. School and teacher, and school and student are linked...
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...
1
by: Andy_Khosravi | last post by:
Background: Access 2003 (converted from A97 DB recently), database is split into FE/BE with the FE residing on client machine. I've got a question about how I can possibly optimize a query I've...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 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 former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
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: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?

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.