Results 1 to 5 of 5
  1. #1
    anthgav is offline Advanced Beginner
    Windows 10 Access 2016
    Join Date
    Sep 2017
    Posts
    86

    Merging 2 queries

    Hi

    I have 2 queries that count items from 2 fields in the same table.

    My first query is called qryBinCapacityMasterBin


    HTML Code:
    SELECT tblBinType.bin_type, Count(tblAllocatedBin.allocated_bin_type) AS CountOfallocated_bin_type, Count(tblProduct.allocated_bin_1) AS
    CountOfallocated_bin_1, Round(Count([tblProduct.allocated_bin_1])/Count([tblAllocatedBin.allocated_bin])*100,1) AS [Capacity%]FROM (tblAllocatedBin LEFT JOIN
    tblProduct ON
    tblAllocatedBin.allocated_bin_id = tblProduct.[allocated_bin_1]) LEFT JOIN tblBinType ON
    tblAllocatedBin.allocated_bin_type = tblBinType.bin_type_idGROUP BY tblBinType.bin_type;
    and produces this result
    Click image for larger version. 

Name:	1.PNG 
Views:	19 
Size:	7.2 KB 
ID:	32322
    My second query is called qryBinCapacitySecondBin
    HTML Code:
    SELECT tblBinType.bin_type, Count(tblAllocatedBin.allocated_bin_type) AS CountOfallocated_bin_type, Count(tblProduct.allocated_bin_2) AS
    CountOfallocated_bin_2, Round(Count([tblProduct.allocated_bin_2])/Count([tblAllocatedBin.allocated_bin])*100,1) AS [Capacity%]FROM (tblAllocatedBin LEFT JOIN
    tblBinType ON
    tblAllocatedBin.allocated_bin_type = tblBinType.bin_type_id) LEFT JOIN tblProduct ON
    tblAllocatedBin.allocated_bin_id = tblProduct.allocated_bin_2GROUP BY tblBinType.bin_type;
    and produces this result
    Click image for larger version. 

Name:	2.PNG 
Views:	19 
Size:	7.0 KB 
ID:	32323

    The 2 queries are identical except they are looking from 2 different fields. I would like to merge the results into one total. I've been trying UNION queries but have not been able to produce the desired result. What is the best way of acheiving this please?

  2. #2
    ranman256's Avatar
    ranman256 is offline VIP
    Windows Vista Access 2010 32bit
    Join Date
    Apr 2014
    Location
    Kentucky
    Posts
    9,525
    make a union query to gather both queries,
    then make a final query to sum the union.
    qnUnion:


    select* from qryBinCapacityMasterBin
    union
    select* from qryBinCapacitySecondBin



    then
    select bin_type, sum(countofAllocated_bin_type),sum(countofAllocate d_bin1) from qnUnion.

  3. #3
    anthgav is offline Advanced Beginner
    Windows 10 Access 2016
    Join Date
    Sep 2017
    Posts
    86
    I've created this union but added UNION ALL instead of just UNION to separate lines as UNION by itself merges the 2 QCEBIN lines but does not add them together.

    HTML Code:
    SELECT bin_type, CountOfallocated_bin_type, CountOfallocated_bin_1 AS allocated_bin
    FROM qryBinCapacityMasterBin
    UNION ALL
    SELECT bin_type, CountOfallocated_bin_type, CountOfallocated_bin_2 AS allocated_bin
    FROM qryBinCapacitySecondaryBin;
    The result is...
    Click image for larger version. 

Name:	3.PNG 
Views:	16 
Size:	10.2 KB 
ID:	32327

    I need to be able to merge all fields but allocated_bin_1 & allocated_bin_2 need to be summed.

    I'm struggling to work out a query to achieve this, what is the best way to achieve this?

  4. #4
    aytee111 is offline Competent At Times
    Windows 10 Access 2013 64bit
    Join Date
    Nov 2011
    Location
    Nomad
    Posts
    3,936
    Create a totals query from the UNION, group by bin type and sum your totals.

  5. #5
    anthgav is offline Advanced Beginner
    Windows 10 Access 2016
    Join Date
    Sep 2017
    Posts
    86
    Thanks guys I finally figured it out with your help, my final query from the union is...

    Code:
    SELECT qryCapacityOfMasterAndSecondaryBins.bin_type, qryCapacityOfMasterAndSecondaryBins.CountOfallocated_bin_type, Sum(qryCapacityOfMasterAndSecondaryBins.allocated_bin) AS SumOfallocated_bin
    FROM qryCapacityOfMasterAndSecondaryBins
    GROUP BY qryCapacityOfMasterAndSecondaryBins.bin_type, qryCapacityOfMasterAndSecondaryBins.CountOfallocated_bin_type;

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

Similar Threads

  1. Replies: 2
    Last Post: 02-29-2016, 03:00 PM
  2. Merging Records.
    By Tom Kidman in forum Forms
    Replies: 1
    Last Post: 05-27-2014, 06:11 AM
  3. Merging two queries of data using outer joins
    By scorpio in forum Queries
    Replies: 1
    Last Post: 02-02-2014, 12:58 PM
  4. Merging 3 queries into one report
    By terricritch in forum Reports
    Replies: 2
    Last Post: 06-23-2010, 02:38 AM
  5. Merging
    By bailey537 in forum Queries
    Replies: 0
    Last Post: 07-14-2009, 04:14 AM

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