Results 1 to 3 of 3
  1. #1
    gmcconville is offline Novice
    Windows 10 Access 2003
    Join Date
    Sep 2017
    Posts
    1

    Stuck on how to create a Union query that groups from two different tables

    Good afternoon,



    It has been about 25 years since I last used databases so I am a little rusty.

    I have the following query...

    SELECT Invoice, SalesID, First(Customer) AS Cust, First(BookoutDate) AS BODt, Sum(Quantity) AS Qty, Sum(Cost) AS Cst, Sum(SellPrice) AS Sell, Sum(GrossProfit) AS GP
    FROM Sales082017
    WHERE Invoice <> "0"
    GROUP BY Invoice, SalesID


    UNION SELECT Invoice, SalesID, First(Customer) AS Cust, First(BookoutDate) AS BODt, Sum(Quantity) AS Qty, Sum(Cost) AS Cst, Sum(SellPrice) AS Sell, Sum(GrossProfit) AS GP
    FROM Sales092017
    WHERE Invoice <> "0"
    GROUP BY Invoice, SalesID;

    Which doesn't quite do what I want it to.

    It groups everything together, but only per month

    EG: I might have the following lines that were meant to be grouped together, but because they are from different tables, they are seperate

    Invoice SalesID Cust BODt Qty Cst Sell GP
    1629418 6316 Sample Customer 16/08/2017 5 $225.78 $208.70 -17.08
    1629418 6316 Sample Customer 1/09/2017 2 -$84.74 -$0.01 84.73


    Any assistance would be great thanks

  2. #2
    June7's Avatar
    June7 is offline VIP
    Windows 7 64bit Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    53,770
    Why do you have two tables with identical structure to begin with?

    Do the UNION and then do another query to do the aggregation. Or build a report using the UNION query as RecordSource and use report Sorting & Grouping features with aggregate calcs in textboxes.

    You probably should use UNION ALL.
    How to attach file: http://www.accessforums.net/showthread.php?t=70301 To provide db: copy, remove confidential data, run compact & repair, zip w/Windows Compression.

  3. #3
    Join Date
    Apr 2017
    Posts
    1,792
    You really have a table for every month???!!! How many of them?

    The best solution will be you create a new empty table and insert all your monthly tables into it. After that your query will be an easy thing.

Please reply to this thread with any new information or opinions.

Similar Threads

  1. Replies: 4
    Last Post: 12-20-2015, 02:35 PM
  2. Replies: 7
    Last Post: 12-17-2015, 04:36 PM
  3. Replies: 1
    Last Post: 10-16-2014, 10:36 PM
  4. Replies: 7
    Last Post: 01-16-2014, 09:17 AM
  5. Create table out of union query
    By DKruse1969 in forum Queries
    Replies: 2
    Last Post: 08-28-2009, 09:55 AM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Other Forums: Microsoft Office Forums