-
October 20th, 2011, 08:07 PM
#1
A question regarding aggregate function
Suppose I have a table A and a table B.
Table A:
Usage Expense Name
Food 100 Jason
Food 150 Mike
Rent 1000 Jenny
Gas 50 Jason
Fun 60 John
Rent 1200 Jason
Gas 30 Jenny
Fun 70 Mike
Rent 900 Mike
Food 200 John
Fun 40 Jenny
Gas 80 Mike
Food 180 Jenny
Rent 800 John
Gas 60 John
Fun 30 Jason
Table B:
Usage Expense Name
Food 100 Bob
Fun 40 Bob
Gas 50 Bob
Rent 1100 Bob
Now if I want to use a query to display total expense in Food from John, Jenny, Mike, Jason and Bob, how can I do that? Thanks.
-
October 21st, 2011, 03:13 AM
#2
Re: A question regarding aggregate function
Use UNION to combine results from both tables. Do you need to get the total expenses by Name or the combined total? If you need the combined total use SUM(Expense) (you will have to use it anyway if there might be more than one Food record with the same Name). If you need individual totals by Name use GROUP BY.
-
October 25th, 2011, 08:00 PM
#3
Re: A question regarding aggregate function
Originally Posted by jcaccia
Use UNION to combine results from both tables. Do you need to get the total expenses by Name or the combined total? If you need the combined total use SUM(Expense) (you will have to use it anyway if there might be more than one Food record with the same Name). If you need individual totals by Name use GROUP BY.
Thanks for your reply. It works! But I still don't understand how UNION adds the results together from two queries?
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|