Results 1 to 2 of 2
  1. #1
    hsduk101 is offline Novice
    Windows 7 32bit Access 2010 32bit
    Join Date
    Jul 2017
    Posts
    2

    Aggregate Results from Union query

    I have 2 queries - with slightly different joins - with the same fields that I am union-ing. My results set give me 2 lines for one 'route'



    QUESTION: How do i Aggregate the duplicated lines (I dont want to aggregate on Success and Count Prevent - these to be summed by group by)?

    I think the solution is to write a Select statement outside this overall select statement - but I dont know how to write this in MS Access 2010

    ......ALSO - Can I used the results from the union query as a sub query into another query (Being designed in Query Designer)

    (CODE BELOW)

    Code:
    SELECT qryRef_Routes_tbl.Ref_Route_Name, qryRef_Routes_tbl.Ref_Route_ID, tblFMS_Data.FM_FinYear, tblFMS_Data.FM_Period, tblPeriod_Lookup.PL_ID, Sum(IIf([FM_TrustNo]=0 And [FM_Incident]=25 And [Ref_Fault_Valid_Combo]=-1,1,0)) AS Success, Sum(IIf([FM_Incident]=25 And [Ref_Fault_Valid_Combo]=-1,1,0)) AS CountPreventFROM (((tblFMS_Data INNER JOIN qryRef_Routes_tbl ON tblFMS_Data.FM_Route = qryRef_Routes_tbl.Ref_Route_ID) INNER JOIN tblRCM_II_Assets ON tblFMS_Data.FM_EllipseNo = tblRCM_II_Assets.RI_Number) INNER JOIN tblPeriod_Lookup ON (tblFMS_Data.FM_FinYear = tblPeriod_Lookup.PL_FinYear) AND (tblFMS_Data.FM_Period = tblPeriod_Lookup.PL_PeriodNo)) INNER JOIN qryRef_Detectable_Failure_Valid_Combo ON (tblFMS_Data.FM_Component2 = qryRef_Detectable_Failure_Valid_Combo.Ref_Fault_Comp2_ID) AND (tblFMS_Data.FM_Component = qryRef_Detectable_Failure_Valid_Combo.Ref_Fault_Comp1_ID)
    GROUP BY qryRef_Routes_tbl.Ref_Route_Name, qryRef_Routes_tbl.Ref_Route_ID, tblFMS_Data.FM_FinYear, tblFMS_Data.FM_Period, tblPeriod_Lookup.PL_ID
    HAVING (((tblPeriod_Lookup.PL_ID) Between [Forms]![frmMainMenu]![PriorPeriodID] And ([Forms]![frmMainMenu]![PriorPeriodID]-12)));
    
    
    
    
    UNION ALL
    
    
    SELECT qryRef_Routes_tbl.Ref_Route_Name, qryRef_Routes_tbl.Ref_Route_ID, tblFMS_Data.FM_FinYear, tblFMS_Data.FM_Period, tblPeriod_Lookup.PL_ID, Sum(IIf([FM_TrustNo]=0 And [FM_Incident]=25 And [Ref_Fault_Valid_Combo]=-1,1,0)) AS Success, Sum(IIf([FM_Incident]=25 And [Ref_Fault_Valid_Combo]=-1,1,0)) AS CountPrevent
    FROM (((tblFMS_Data INNER JOIN qryRef_Routes_tbl ON tblFMS_Data.FM_Route = qryRef_Routes_tbl.Ref_Route_ID) INNER JOIN tblPeriod_Lookup ON (tblFMS_Data.FM_FinYear = tblPeriod_Lookup.PL_FinYear) AND (tblFMS_Data.FM_Period = tblPeriod_Lookup.PL_PeriodNo)) INNER JOIN qryRef_Detectable_Failure_Valid_Combo ON (tblFMS_Data.FM_Component2 = qryRef_Detectable_Failure_Valid_Combo.Ref_Fault_Comp2_ID) AND (tblFMS_Data.FM_Component = qryRef_Detectable_Failure_Valid_Combo.Ref_Fault_Comp1_ID)) INNER JOIN tblRCM_II_Assets ON tblFMS_Data.FM_EllipseNo = tblRCM_II_Assets.RI_Pway_Number
    GROUP BY qryRef_Routes_tbl.Ref_Route_Name, qryRef_Routes_tbl.Ref_Route_ID, tblFMS_Data.FM_FinYear, tblFMS_Data.FM_Period, tblPeriod_Lookup.PL_ID
    HAVING (((tblPeriod_Lookup.PL_ID) Between [Forms]![frmMainMenu]![PriorPeriodID] And ([Forms]![frmMainMenu]![PriorPeriodID]-12)));

  2. #2
    pbaldy's Avatar
    pbaldy is offline Who is John Galt?
    Windows XP Access 2007
    Join Date
    Feb 2010
    Location
    Nevada, USA
    Posts
    22,652
    You can create a new query based on the union query. In design view, just add the query to the grid just like you would add a table (queries are on a separate tab).
    Paul (wino moderator)
    MS Access MVP 2007-2019
    www.BaldyWeb.com

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

Similar Threads

  1. Replies: 2
    Last Post: 06-21-2014, 07:11 AM
  2. Replies: 6
    Last Post: 03-29-2013, 11:05 AM
  3. Replies: 1
    Last Post: 10-23-2012, 02:04 PM
  4. Replies: 8
    Last Post: 10-22-2012, 07:43 PM
  5. Replies: 3
    Last Post: 05-21-2010, 03:57 PM

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