CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 3 of 3
  1. #1
    Join Date
    Jul 2005
    Posts
    1,030

    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.

  2. #2
    Join Date
    May 2009
    Location
    Lincs, UK
    Posts
    298

    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.

  3. #3
    Join Date
    Jul 2005
    Posts
    1,030

    Re: A question regarding aggregate function

    Quote Originally Posted by jcaccia View Post
    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
  •  





Click Here to Expand Forum to Full Width

Featured