Results 1 to 2 of 2
  1. #1
    joewilly1 is offline Novice
    Windows XP Access 2003
    Join Date
    Nov 2010
    Posts
    2

    Union and Sum all in one query


    Seems similar questions have been asked but I haven't found the exact answer to my problem. I have two tables with the same exact structure that were imported from Excel. The fields would be something like Product, Jan, Feb, etc. There's a table that specifically has sales for Jan, a separate one for Feb and so on. I want a table that shows sales for a product for each month. When I do a union query I get a table that has record for Product A with Jan sales and zeros for the other months. There's another record for Product A with Feb sales and zeros in the other months and so on. I want one record for Product A with sales for each month. It's pretty easy to do a union query and then a sum query but I was wondering if it could be done in one query instead of two.

  2. #2
    Robeen is offline VIP
    Windows XP Access 2010 32bit
    Join Date
    Mar 2011
    Location
    Tulsa, Oklahoma.
    Posts
    1,596
    I think you will need a CrossTab query to make your data come out the way you want.

    Best solution I can come up with is a 2-step solution like this:
    1. UNION ALL Query ["QrySales_A_B_C"] with data from each of the tables grouped:
    Code:
    SELECT "1_Jan" AS SaleMonth, Sales_A.Product, Sum(Sales_A.Amount) AS Amt
    FROM Sales_A
    GROUP BY "1_Jan", Sales_A.Product
    UNION ALL
    SELECT "2_Feb" AS SaleMonth, Sales_B.Product, Sum(Sales_B.Amount) AS Amt
    FROM Sales_B
    GROUP BY "2_Feb", Sales_B.Product
    UNION ALL
    SELECT "3_Mar" AS SaleMonth, Sales_C.Product, Sum(Sales_C.Amount) AS Amt
    FROM Sales_C
    GROUP BY "3_Mar", Sales_C.Product;
    2. CrossTab Query using the above query to get your data looking the way you need it to:
    Code:
    TRANSFORM Sum(QrySales_A_B_C.[Amt]) AS SumOfAmt
    SELECT QrySales_A_B_C.[Product]
    FROM QrySales_A_B_C
    GROUP BY QrySales_A_B_C.[Product]
    PIVOT QrySales_A_B_C.[SaleMonth];
    * I know using "1_Jan", 2_Feb" . . . is probably not the most elegant way to do this but I can't think of another way without addin steps.
    ** I used the Query Wizard to create a CrossTab Query.

    I added screenshots to clarify . . .

    I hope this helps!!
    Attached Thumbnails Attached Thumbnails Tables.JPG   QueryResults.JPG  
    Last edited by Robeen; 10-12-2012 at 10:10 AM. Reason: Added screenshots.

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

Similar Threads

  1. Union Query Help
    By pmp in forum Queries
    Replies: 4
    Last Post: 10-28-2011, 06:41 AM
  2. Help with a Union Query
    By Bear in forum Queries
    Replies: 12
    Last Post: 08-14-2011, 05:12 PM
  3. Union Query
    By jlclark4 in forum Queries
    Replies: 3
    Last Post: 02-25-2011, 08:21 PM
  4. Union Query Help
    By jo15765 in forum Queries
    Replies: 7
    Last Post: 01-06-2011, 05:46 PM
  5. Need help with a Union Query
    By jdowdy in forum Access
    Replies: 1
    Last Post: 10-13-2009, 05:24 PM

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