Solved

Get information from multiple queries without records multiplying

Posted on 2011-09-25
7
269 Views
Last Modified: 2012-08-13
Hi Experts, hopefully I will be able to explain this and make sense.

I have a number of queries which each contain data I require, they are all based on the same initial dataset, but, each query calculates different things. So I have

Query 1 which calculates number of inspections
Query 2 which calculates number of inspections complete
Query 3 which calculates number of emergency inspections
Query 4 which calculates number of emergency inspections complete

All queries contain the user id as the identifying field e.g.
Query 1
User ID    Num Inspections
Cane12          412
Brandy34       321

Query 2
User ID    Num Inspections complete
Cane12          389
Brandy34       145

etc....

I want to pull all this data together in a single query so it gives me
End Query
User ID    Num Inspections  Num Inspections complete
Cane12          412                                 389
Brandy34       321                                 145


However, what I am currently getting is:
End Query
User ID    Num Inspections  Num Inspections complete
Cane12          412                                 389
Cane12          412                                 145
Brandy34       321                                 389
Brandy34       321                                 145

Can anyone please help me out with this, I am getting desperate. Please make any explanation fairly simple as I am a very new user. Many thanks in advance :)
0
Comment
Question by:LlMa
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 2
7 Comments
 
LVL 77

Accepted Solution

by:
peter57r earned 500 total points
ID: 36595069
In the query that 'brings it all together' you should be joining all four queries in a chain , using the Userid as the join field from one to the next.
0
 
LVL 92

Expert Comment

by:Patrick Matthews
ID: 36595075
You may not even need to have 4 separate source queries.

Please post the SQL statements for the queries you have now.
0
 

Author Comment

by:LlMa
ID: 36595084
Peter I have done that and it didn't make any difference :(

Mathew shall do that now
0
MS Dynamics Made Instantly Simpler

Make Your Microsoft Dynamics Investment Count  & Drastically Decrease Training Time by Providing Intuitive Step-By-Step WalkThru Tutorials.

 

Author Comment

by:LlMa
ID: 36595096
Mathew here is the SQL code

KnownInsp
SELECT qryWeeklyInspectionUpdateSub.Year, qryWeeklyInspectionUpdateSub.UserId, Count(qryWeeklyInspectionUpdateSub.inMethod) AS [Known Inspections], qryWeeklyInspectionUpdateSub.Emergency
FROM qryWeeklyInspectionUpdateSub
GROUP BY qryWeeklyInspectionUpdateSub.Year, qryWeeklyInspectionUpdateSub.UserId, qryWeeklyInspectionUpdateSub.Emergency
HAVING (((qryWeeklyInspectionUpdateSub.Emergency)=No));

EmergencyInsp
SELECT qryWeeklyInspectionUpdateSub.Year, qryWeeklyInspectionUpdateSub.UserId, Count(qryWeeklyInspectionUpdateSub.inMethod) AS [Emergent Inspections], qryWeeklyInspectionUpdateSub.Emergency
FROM qryWeeklyInspectionUpdateSub
GROUP BY qryWeeklyInspectionUpdateSub.Year, qryWeeklyInspectionUpdateSub.UserId, qryWeeklyInspectionUpdateSub.Emergency
HAVING (((qryWeeklyInspectionUpdateSub.Emergency)=Yes));

InspComplete – This has to have a small sub query to determine if the inspection is complete as there are a number of different complete statuses
SELECT qryWeeklyUpdateCompInspsSub.Year, qryWeeklyUpdateCompInspsSub.UserId, Sum(qryWeeklyUpdateCompInspsSub.CountOfinMethod) AS [Complete Inspections]
FROM qryWeeklyUpdateCompInspsSub
GROUP BY qryWeeklyUpdateCompInspsSub.Year, qryWeeklyUpdateCompInspsSub.UserId;
Sub for that
SELECT qryWeeklyInspectionUpdateSub.Year, qryWeeklyInspectionUpdateSub.UserId, Count(qryWeeklyInspectionUpdateSub.inMethod) AS CountOfinMethod, qryWeeklyInspectionUpdateSub.inStatus
FROM qryWeeklyInspectionUpdateSub
GROUP BY qryWeeklyInspectionUpdateSub.Year, qryWeeklyInspectionUpdateSub.UserId, qryWeeklyInspectionUpdateSub.inStatus
HAVING (((qryWeeklyInspectionUpdateSub.inStatus) Like "Insp Comp*"));

EmergencyInspComplete – As before, this has to have a small sub query to determine if the inspection is complete as there are a number of different complete statuses
SELECT qryWeeklyUpdateEmergCompSub.Year, qryWeeklyUpdateEmergCompSub.UserId, Sum(qryWeeklyUpdateEmergCompSub.CountOfinMethod) AS [Complete Emergent Inspections]
FROM qryWeeklyUpdateEmergCompSub
GROUP BY qryWeeklyUpdateEmergCompSub.Year, qryWeeklyUpdateEmergCompSub.UserId;
Sub for that
SELECT qryWeeklyInspectionUpdateSub.Year, qryWeeklyInspectionUpdateSub.UserId, Count(qryWeeklyInspectionUpdateSub.inMethod) AS CountOfinMethod, qryWeeklyInspectionUpdateSub.inStatus, qryWeeklyInspectionUpdateSub.Emergency
FROM qryWeeklyInspectionUpdateSub
GROUP BY qryWeeklyInspectionUpdateSub.Year, qryWeeklyInspectionUpdateSub.UserId, qryWeeklyInspectionUpdateSub.inStatus, qryWeeklyInspectionUpdateSub.Emergency
HAVING (((qryWeeklyInspectionUpdateSub.inStatus) Like "Insp Comp*") AND ((qryWeeklyInspectionUpdateSub.Emergency)=Yes));
0
 

Author Comment

by:LlMa
ID: 36595113
I think I have just cracked it - though it would still be nice to have all that in one

I added the user id table and had all four queries linked to that instead and it works :D
0
 
LVL 92

Expert Comment

by:Patrick Matthews
ID: 36595592
LlMa,

Glad to hear you got it working.  Your approach is essentially the same thing as what Pete posted in http:#a36595069, so I recommend you select his answer unless the performance using your work-around is unacceptable.

Patrick
0
 

Author Closing Comment

by:LlMa
ID: 36598109
Thanks for your help Peter, it led me to my solution :)

Thanks Patrick :)
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Access custom database properties are useful for storing miscellaneous bits of information in a format that persists through database closing and reopening.  This article shows how to create and use them.
The Windows Phone Theme Colours is a tight, powerful, and well balanced palette. This tiny Access application makes it a snap to select and pick a value. And it doubles as an intro to implementing WithEvents, one of Access' hidden gems.
In Microsoft Access, learn different ways of passing a string value within a string argument. Also learn what a “Type Mis-match” error is about.
Access reports are powerful and flexible. Learn how to create a query and then a grouped report using the wizard. Modify the report design after the wizard is done to make it look better. There will be another video to explain how to put the final p…

724 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