Hi,

I have a program that runs a complex query to collect and group records:
Code:
    sSQL = "SELECT Int([Dt]-#" & dtStart & "#)*24+Hour([Dt]-#" & dtStart & "#) AS [Hour],  [MR].ZoneID, sum(IIf(tblOutages!GID Is Null,[MR].Capacity,0)) AS SumOfCapacity " & _
            "FROM " & _
            "(SELECT tblDt.Dt, tblGx.GID, tblGx!Installed_Capacity*(1-tblGx![EFOR(%)]/100) AS Capacity,  tblGx.ZoneID " & _
            "FROM tblDt, tblGx " & _
           "WHERE Dt BETWEEN #" & dtStart & "# And #" & dtEnd & "# AND ((tblGx.ResID)=3 Or (tblGx.ResID)=6 Or (tblGx.ResID)=7) And tblGx.InServiceDate<Dt And tblGx.OutServiceDate>Dt " & _
           "UNION ALL SELECT " & _
           "tblDt.Dt , tblGx.GID, tblHydroProfiles.Capacity, tblGx.ZoneID " & _
           "FROM tblGx INNER JOIN (tblDt INNER JOIN tblHydroProfiles ON (tblDt.Hour=tblHydroProfiles.Hour) AND (tblDt.DOW=tblHydroProfiles.DOW) AND (tblDt.Month=tblHydroProfiles.Month)) ON tblGx.ProfileID=tblHydroProfiles.HydroProfileID " & _
           "WHERE (((tblGx.InServiceDate)<=tblDt!Dt) And ((tblGx.OutServiceDate)>tblDt!Dt)) AND tblDt.Dt BETWEEN #" & dtStart & "# And #" & dtEnd & "# " & _
           "UNION ALL SELECT " & _
           "tblDt.Dt , tblGx.GID, tblGx.Installed_Capacity * tblWindProfile.CapFac, tblGx.ZoneID " & _
           "FROM tblDt INNER JOIN (tblGx INNER JOIN tblWindProfile ON tblGx.ProfileID=tblWindProfile.WindProfileID) ON (tblDt.Month=tblWindProfile.Month) AND (tblDt.Hour=tblWindProfile.Hour) " & _
           "WHERE (((tblGx.InServiceDate)<=tblDt!Dt) And ((tblGx.OutServiceDate)>tblDt!Dt)) AND tblDt.Dt BETWEEN #" & dtStart & "# And #" & dtEnd & "#" & _
            ") AS MR LEFT JOIN tblOutages ON (MR.GID = tblOutages.GID) AND (MR.Dt >= tblOutages.StartDate) AND (MR.Dt <= tblOutages.EndDate) " & _
            "GROUP BY tblDt.Dt, tblGx.ZoneID;"

However, before the final "GROUP BY tblDt.Dt, tblGx.ZoneID" statement, I would like to first "GROUP BY tblGx.GID" to find the sums over the period (tblDt.Dt). Should I first run a MAKETABLE query, perform the two outer queries separately, and then run a DELETE query (and compact and repair) ?