Hi Wayne,
Thanks for the reply. I am testing on a standalone computer and using SQL command for ADO recordset. I used nested LEFT JOIN before and now I reduce it to a single LEFT JOIN. It is faster now - but not as I expect yet. It seems that this join makes the query slow. In fact some of the keys are texts (IDHealthFacility,IDVillage,IDDiagCode) and you said it is inefficient. Below is the SQL command:
INSERT INTO TableRecapMonthlyReport(IDHealthFacility,IDVillage,[Month],[Year],IDDiagCode,IDAgeGroup,TotalNewCase,TotalOldCase,TotalGeneralCase) SELECT '" & listData.Value & "' AS IDHF, 'DUMMY' AS IDV, " & RecapMonth & " AS Mo, " & RecapYear & " AS Yr, '" & sRec.Fields(0) & "' AS IDDC, " & sRec2.Fields(0) & " AS IDAG, SUM(TableMonthlyLB1.TotalNewCase), SUM(TableMonthlyReport.TotalOldCase), SUM(TableMonthlyReport.TotalGeneralCase) FROM TableMonthlyReport LEFT JOIN TableHealthFacilityInfo ON TableMonthlyReport.IDHealthFacility = TableHealthFacilityInfo.IDHealthFacility WHERE TableMonthlyReport.Month=" & RecapMonth & " AND TableMonthlyReport.Year=" & RecapYear & " AND TableHealthFacilityInfo.IDHealthCenter='" & listData.Value & "' AND TableMonthlyReport.IDDiagCode='" & sRec.Fields(0).Value & "' AND TableMonthlyReport.IDAgeGroup=" & sRec2.Fields(0).Value