Solved

Combining results from multiple queries in ACCESS

Posted on 2014-03-31
9
1,136 Views
Last Modified: 2014-04-01
Experts,

I have to tables in Access one is Boys and the other Girls.
I would like to get the total number from each table.
i.e.
 Select count(*) as Total_Girls from Girls;
Select count(*) as Total_Boys from Boys;

I would like the results to be in the same table.

Is there a way to do this in Access?
0
Comment
Question by:morinia
  • 3
  • 3
  • 2
  • +1
9 Comments
 
LVL 34

Expert Comment

by:PatHartman
ID: 39967003
I would like the results to be in the same table
Do you mean query?  Do you want them to be in the same row or separate rows?

You might start with the original data and do a query on that.

Select Sum(IIf(SexCD = "F", 1, 0)) As GirlsCount, Select Sum(IIf(SexCD = "M", 1, 0)) As BoysCount
From yourOriginalTable;
0
 
LVL 49

Accepted Solution

by:
Gustav Brock earned 250 total points
ID: 39967041
Use a union query:

Select
    "Girl" As Gender,
    Count(*) As Total
From
    Girls
Union
Select
    "Boy" As Gender,
    Count(*) As Total
From
    Boys;

/gustav
0
 

Author Comment

by:morinia
ID: 39967081
Experts,

I want them on separate rows.  The problem I am having now is I only want the using distinct in count in Access.
0
 
LVL 49

Expert Comment

by:Gustav Brock
ID: 39967084
That's what the union query does.

/gustav
0
Ransomware-A Revenue Bonanza for Service Providers

Ransomware – malware that gets on your customers’ computers, encrypts their data, and extorts a hefty ransom for the decryption keys – is a surging new threat.  The purpose of this eBook is to educate the reader about ransomware attacks.

 
LVL 34

Expert Comment

by:PatHartman
ID: 39967096
Go back to the original table.

Select SexCD, Count(*) as RecCount from YourTable;

You'll end up with
Girls 12343
Boys 12344
0
 

Author Comment

by:morinia
ID: 39967187
Experts,

I am sorry for the confusion.  My problem is not concatenating the totals into one table now.  The problem I am having is counting unique values for the respective tables. It is possible to have multiple records with the same key value.  I want to count a person only once.

How would I get distinct values in an access query?
0
 
LVL 49

Expert Comment

by:Gustav Brock
ID: 39967256
> Is it possible to have multiple records with the same key value.  

You keep asking this, but yes, that's what the union query does.

of course, if a person exists in more than one row of the source table, you'll have use two distinct queries as source for the union query rather than the original tables.

/gustav
0
 
LVL 47

Expert Comment

by:Dale Fye (Access MVP)
ID: 39967361
Unique values of what?  For the Girls, try something like:

Select
    "Girl" As Gender,
    Count(DistinctGirls.*) As Total
From (
SELECT DISTINCT [LastName], [FirstName], [DOB]
FROM Girls) as DistinctGirls

If that gets you what you are looking for, then do the same for the boys and UNION them together.
0
 
LVL 34

Expert Comment

by:PatHartman
ID: 39967364
We are flying blind here.  Sounds like you have a table of people and a table of transactions and you want to count by gender the number of people who have had a transaction.

Start with the transaction table
Select Distinct PersonID, Gender From transaction table.

Then create a second query to count the records from the first one.

Select GenderCD, Count(*) as RecCount from firstquery;

You can do this as a query with a sub query but two separate queries are usually easier to test.

If Gender isn't in the transaction table you will need to join to the persons table in one of the queries to pick it up.
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Syntax Error in Query 7 33
Search Form with Run Time Error 3075 1 23
MS Access 03, TransferText, decimal places 8 47
Getting Run-Time Error 13 - Type Mismatch 3 28
Introduction The Visual Basic for Applications (VBA) language is at the heart of every application that you write. It is your key to taking Access beyond the world of wizards into a world where anything is possible. This article introduces you to…
In a multiple monitor setup, if you don't want to use AutoCenter to position your popup forms, you have a problem: where will they appear?  Sometimes you may have an additional problem: where the devil did they go?  If you last had a popup form open…
Familiarize people with the process of utilizing SQL Server views from within Microsoft Access. Microsoft Access is a very powerful client/server development tool. One of the SQL Server objects that you can interact with from within Microsoft Access…
Familiarize people with the process of utilizing SQL Server functions from within Microsoft Access. Microsoft Access is a very powerful client/server development tool. One of the SQL Server objects that you can interact with from within Microsoft Ac…

863 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

26 Experts available now in Live!

Get 1:1 Help Now