473,396 Members | 2,129 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,396 software developers and data experts.

Counting of records in a union query

I have about 20 tables, of which I would like to do a union query and
count of how many of each different code there is?

The simplified verson of the table is structured like this.

Code Count
1234 1
2468 1
1234 1
2468 1
1234 1
1111 1
1234 1
2468 1
1111 1
2222 1

I would like to have it in this format

1111 2
1234 4
2222 1
2468 3

The tables are all approx 60K each in size, so an append query would
slow down the database too much, as they are all referenced to a
particular time period.

How would I write a Union query to calculate this?

Also, is it possible to link two fields from two different tables in a
union query, ie if the above numbers were linked in another table, then
return the other specified field.

IE from the tables above, using the below table to convert to the final
table below.

1111 Blue
1234 Green
2222 Red
2468 Orange

Blue 2
Green 4
Red 1
Orange 3

I would appreciate any assistance anyone can provide me!

Cheers,

Mattytee

Nov 13 '05 #1
2 4315
ma*********@hotmail.com wrote in
news:11**********************@f14g2000cwb.googlegr oups.com:
I have about 20 tables, of which I would like to do a union query
and count of how many of each different code there is?

The simplified verson of the table is structured like this.

EXAMPLE 1 Code Count
1234 1
2468 1
1234 1
2468 1
1234 1
1111 1
1234 1
2468 1
1111 1
2222 1

I would like to have it in this format
EXAMPLE 2 1111 2
1234 4
2222 1
2468 3

The tables are all approx 60K each in size, so an append query
would slow down the database too much, as they are all referenced
to a particular time period.

How would I write a Union query to calculate this?
I don't see where you need a UNION query. I think you actually mean
a join. A UNION query takes several individual queries, often based
on different tables, and assembles the separate sets of records into
a single recordset:

SELECT tblFirstTable.Field1, tblFirstTable.Field2
FROM tblFirstTable

UNION ALL SELECT tblSecondTable.Field1, tblSecondTable.Field2
FROM tblSecondTable;

If tblFirstTable has 10 records and tblSecondTable has 10, you'd end
up with 20 records. If there are duplicates between the two tables
and you want to remove those, you'd change UNION ALL to just UNION.
Also, is it possible to link two fields from two different tables
in a union query, ie if the above numbers were linked in another
table, then return the other specified field.

IE from the tables above, using the below table to convert to the
final table below.

EXAMPLE 3 1111 Blue
1234 Green
2222 Red
2468 Orange
EXAMPLE 4 Blue 2
Green 4
Red 1
Orange 3

I would appreciate any assistance anyone can provide me!


I'm not sure I understand what you're asking for. Your 2nd example
(I've put labels on them) looks like it totals the data in the 1st
example. I think you're meaning to give color codes to the counts,
in which case, you'd do something like this:

SELECT Code, Count(*) As CodeCount
FROM tblCodeList

Save that as a query, say qryCodeCount.

Then create another query and drop the table with the color lookup
in it (example 3) and then drop qryCodeCount onto it. Then drag the
field CodeCount to the 2nd column of the color lookup table. The
result should be SQL that looks something like this:

SELECT qryCodeCount.Code, tblColorLookup.Color
FROM qryCodeCount INNER JOIN tblColorLookup
ON qryCodeCount.Count = tblColorLookup.ColorID

Now, that's probably erroneous SQL (I've never learned how to
manually type joins -- I always use the QBE designer to write the
joins for me), but it will be something like that.

The result would be a result that looks like your example 3.

--
David W. Fenton http://www.bway.net/~dfenton
dfenton at bway dot net http://www.bway.net/~dfassoc
Nov 13 '05 #2
ma*********@hotmail.com wrote:
I have about 20 tables, of which I would like to do a union query and
count of how many of each different code there is?


From the rest of your message, I am under the impression that you have
20 tables with exactly the same structure?

If so, the question is W H Y, in the name of all that's holy, are these
not a single table?

If I'm wrong, disregard this transmission! 8)

--
Tim http://www.ucs.mun.ca/~tmarshal/
^o<
/#) "Burp-beep, burp-beep, burp-beep?" - Quaker Jake
/^^ "Whatcha doin?" - Ditto "TIM-MAY!!" - Me
Nov 13 '05 #3

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

Similar topics

2
by: M Stery | last post by:
Have two base queries used for mailing labels with id,name,address,city,state,zip that are combined via a union query. One base query uses home address; the other base query uses biz address. I...
0
by: Jason | last post by:
I have a primary form which is used to enter/edit data in a table named Test_Results. On this primary form there is a subform which displays site addresses. This subform is linked to the primary...
1
by: snOOp | last post by:
I am trying to combine the data from two similar tables into one query, but I need for all of the records from both tables to show up and I want the ones that have matching 'emplid' to be combined...
1
by: Brian | last post by:
I'm trying to find a way to search multiple tables for the same record. Say I have 3 tables all with a name column, I need to search all 3 tables and find matching names. Is there an easy way to...
1
by: Dixie | last post by:
I have 4 fields (byte) which can each contain a number between 1 and 40 odd.. The numbers in each of the fields represents the same information that can occur in one of 4 places. In each set of...
2
by: Zeljko | last post by:
I'm creating Address book. Header of the main form (frmAddress) contains combo box (cboFilter) to filter records by Occupation on main Form(Ocupation1). That's working. Combo box also have "Show...
9
by: Ericka | last post by:
I used the following query to count the number of records from each table. Select "Employee_Table" as Table_Name, Count(*) As Record_Total From Employer_table UNION ALL
2
kcdoell
by: kcdoell | last post by:
Hello: I am trying to create a union query but do not have a lot of experience. Basically I have the below tables: The Tables: Table Name = tblPrior CreditRegIDFK; Number; Foreign Key...
0
by: Hamayun Khan | last post by:
Hi All I am using the following query to select records from two tables. Select JobTitle,JobDesc,Scraped,logoimage,JobPostID,SchoolID,web,MemType,InstitutionName,PayScale,LEA,Contract as...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
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...
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
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
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.