I have the following query that I am building through and ASP page based on a user selection. The info is being pulled from a SQL server db. I am being told that there are too many joins and aggregates. I know there are, but I don’t know of any other way to pull the information I need for one report. I don’t have access to the db or the the dba to use the tools to see how the query is running and what might make it better. Any ideas?
sqlQry1="Select schools.site, schools.area, avg(total_enrollment), avg(d_free_apr), "
sqlQry1= sqlQry1 & " avg(sc_free_Meals), avg(d_Redu_apr), avg(sc_redu_meals), "
sqlQry1= sqlQry1 & " avg(sc_d_paid_meals), sc_meal_type, schcat, avg(sc_emp_meals), "
sqlQry1= sqlQry1 & " avg(d_stud_adult_meals_cash), avg(d_tot_meals), "
sqlQry1= sqlQry1 & " avg(d_stud_adult_Ala_meals), schoolName, avg(fc_free_apr), "
sqlQry1= sqlQry1 & " avg(fc_reduced_apr), avg(fc_paid_apr), "
sqlQry1= sqlQry1 & " avg(fc_alac_cash), avg(tot_meals), avg(fc_adult_cash)"
sqlQry1= sqlQry1 & " FROM (((schools inner join tbl_scanned_sales on schools.site = "
sqlQry1= sqlQry1 & " tbl_scanned_sales.sc_site) inner join enrollment on schools.site = "
sqlQry1= sqlQry1 & " enrollment.cus_site) LEFT OUTER join tbl_forecast on schools.site = "
sqlQry1= sqlQry1 & " tbl_forecast.site and sc_meal_type=meal_type) "
sqlQry1= sqlQry1 & " WHERE sc_sales_date between '" & fsale_date & "' and '" & tsale_date & "' "
If optCH = 1 then sqlQry1 = sqlQry1 & "and sc_site = '" & txtSite & "'" End if
If optCH = 3 then sqlQry1 = sqlQry1 & "and schools.area='" & txtarea & "'" End if
If optCH = 5 then sqlQry1 = sqlQry1 & "and lower(right(tbl_scanned_sales.area,1))='" & selReg & "'" End if
If optCH = 7 then sqlQry1 = sqlQry1 & "and schcat ='" & schcat & "'" End if
sqlQry1= sqlQry1 & " and fc_program = 'bfast_lunch'"
sqlQry1=sqlQry1 & " group by schools.site, schools.area, schcat, sc_meal_type, SchoolName"
sqlQry1="Select schools.site, schools.area, avg(total_enrollment), avg(d_free_apr), "
sqlQry1= sqlQry1 & " avg(sc_free_Meals), avg(d_Redu_apr), avg(sc_redu_meals), "
sqlQry1= sqlQry1 & " avg(sc_d_paid_meals), sc_meal_type, schcat, avg(sc_emp_meals), "
sqlQry1= sqlQry1 & " avg(d_stud_adult_meals_cash), avg(d_tot_meals), "
sqlQry1= sqlQry1 & " avg(d_stud_adult_Ala_meals), schoolName, avg(fc_free_apr), "
sqlQry1= sqlQry1 & " avg(fc_reduced_apr), avg(fc_paid_apr), "
sqlQry1= sqlQry1 & " avg(fc_alac_cash), avg(tot_meals), avg(fc_adult_cash)"
sqlQry1= sqlQry1 & " FROM (((schools inner join tbl_scanned_sales on schools.site = "
sqlQry1= sqlQry1 & " tbl_scanned_sales.sc_site) inner join enrollment on schools.site = "
sqlQry1= sqlQry1 & " enrollment.cus_site) LEFT OUTER join tbl_forecast on schools.site = "
sqlQry1= sqlQry1 & " tbl_forecast.site and sc_meal_type=meal_type) "
sqlQry1= sqlQry1 & " WHERE sc_sales_date between '" & fsale_date & "' and '" & tsale_date & "' "
If optCH = 1 then sqlQry1 = sqlQry1 & "and sc_site = '" & txtSite & "'" End if
If optCH = 3 then sqlQry1 = sqlQry1 & "and schools.area='" & txtarea & "'" End if
If optCH = 5 then sqlQry1 = sqlQry1 & "and lower(right(tbl_scanned_sales.area,1))='" & selReg & "'" End if
If optCH = 7 then sqlQry1 = sqlQry1 & "and schcat ='" & schcat & "'" End if
sqlQry1= sqlQry1 & " and fc_program = 'bfast_lunch'"
sqlQry1=sqlQry1 & " group by schools.site, schools.area, schcat, sc_meal_type, SchoolName"