Free Lessons
Courses
Seminars
TechHelp
Fast Tips
Templates
Topic Index
Forum
ABCD
 
Home   Courses   TechHelp   Help   Contact   Merch   Join   Order   Logon   Forums   
 
Back to Access Forum    Comments List
Upload Images   @Reply   Bookmark    Link   Email   Next Unseen 
Combine two aggregate queries
Mandy Duncan 
       
2 years ago
I have two aggregate queries.  They look like this:

1.  WBS, WorkOrder, Description, SumPlan, SumRemaining
2.  WBS, WorkOrder, Description, SumSpent

Many of the work orders will be repeated in both queries but they could have workorders on one that's not on the other.  How can I get results below without duplicating or eliminating any workorders and without duplicating any amounts?

WBS, Workorder, Description, SumPlan, SumRemaining, SumSpent

Thank you.
Kevin Robertson  @Reply  
          
2 years ago
Adam Schwanz  @Reply  
           
2 years ago
Judging from the field names, do these involve different tables? It looks like it could just be one query potentially.

If not, you're probably looking at a Union Query
Mandy Duncan OP  @Reply  
       
2 years ago
It's two different tables, unfortunately.
Mandy Duncan OP  @Reply  
       
2 years ago
Union isn't working.  I can get the select query to work doing a "0 AS" for the missing fields in both queries but they won't union.
Adam Schwanz  @Reply  
           
2 years ago
What do you mean they won't union? Are you getting an error or anything? You will need to fill in missing fields with 0 As SumSpent kind of thing
Adam Schwanz  @Reply  
           
2 years ago
If nothing else you can just DLookup the SumSpent from the other query into it
Mandy Duncan OP  @Reply  
       
2 years ago
Works:
SELECT CJI3SumByWOQ.WBSelement, CJI3SumByWOQ.Order, CJI3SumByWOQ.Description, 0 AS Plan, 0 AS Remaining, CJI3SumByWOQ.Spent
FROM CJI3SumByWOQ
ORDER BY CJI3SumByWOQ.Order;
Works:
SELECT CJI5SumByWOQ.WBSelement, CJI5SumByWOQ.Order, CJI5SumByWOQ.Description, CJI5SumByWOQ.Plan, CJI5SumByWOQ.Remaining, 0 AS Spent
FROM CJI5SumByWOQ
ORDER BY CJI5SumByWOQ.Order;

I get "Syntax error in ORDER BY clause." error if I union them.  I get that error with or without the "order by" SQL above.
Richard Rost  @Reply  
          
2 years ago
Make 2 queries that have exactly the same field names and incorporate your 0 values. Do not include any ordering.

Make your UNION query. No ordering.

Make another query based on your UNION query that handles the ordering.

Could be a classic example of just needing to break it out into multiple steps. Also make sure all of your field data types are the same.
Kevin Robertson  @Reply  
          
2 years ago
Remove the Query names.

SELECT WBSelement, Order, Description, 0 AS Plan, 0 AS Remaining, Spent
FROM CJI3SumByWOQ
ORDER BY Order

UNION

SELECT WBSelement, Order, Description, Plan, Remaining, 0 AS Spent
FROM CJI5SumByWOQ
ORDER BY Order


The field Order may also be causing a problem. If so, you may need to alias it.
Mandy Duncan OP  @Reply  
       
2 years ago
It isn't working.  Could the field name "order" be throwing it off?  

The initial CJI3 table is a linked excel table.  Could that be the issue?
Richard Rost  @Reply  
          
2 years ago
Yes, and yes. I'd import the data into a local table. And definitely alias Order.
Mandy Duncan OP  @Reply  
       
2 years ago
I changed "order" to "Workorder" and that fixed the error I was getting but then I had duplicate records.  It was showing two lines for everything that was in both tables.  So I created another aggregate query to sum them.  Works great!

Importing the table didn't help with the duplicates so I left it linked.

Thanks so much for your help!!!

This thread is now CLOSED. If you wish to comment, start a NEW discussion in Access Forum.
 

Next Unseen

 
New Feature: Comment Live View
 
 

The following is a paid advertisement
Computer Learning Zone is not responsible for any content shown or offers made by these ads.
 

Learn
 
Access - index
Excel - index
Word - index
Windows - index
PowerPoint - index
Photoshop - index
Visual Basic - index
ASP - index
Seminars
More...
Customers
 
Login
My Account
My Courses
Lost Password
Memberships
Student Databases
Change Email
Info
 
Latest News
New Releases
User Forums
Topic Glossary
Tips & Tricks
Search The Site
Code Vault
Collapse Menus
Help
 
Customer Support
Web Site Tour
FAQs
TechHelp
Consulting Services
About
 
Background
Testimonials
Jobs
Affiliate Program
Richard Rost
Free Lessons
Mailing List
PCResale.NET
Order
 
Video Tutorials
Handbooks
Memberships
Learning Connection
Idiot's Guide to Excel
Volume Discounts
Payment Info
Shipping
Terms of Sale
Contact
 
Contact Info
Support Policy
Mailing Address
Phone Number
Fax Number
Course Survey
Email Richard
[email protected]
Blog RSS Feed    YouTube Channel

LinkedIn
Copyright 2026 by Computer Learning Zone, Amicron, and Richard Rost. All Rights Reserved. Current Time: 8/8/2026 6:23:49 AM. PLT: 1s