Hi there! i have this query that we've been using for the past month, its a nice query where it provides me all the details i need for a report though my main problem is that i can't optimize it properly here's my query: WITH TotalRecord(location1, totalrecords, brgyindex)as( select Realproperty.location, COUNT (tdno), brgyIndex from RPTLedger join RealProperty on (RPTLedger.realPropertyId = RealProperty.objid) group by RealProperty.location, brgyIndex),TotalPending (location2, totalpending, brgyindex)as( select distinct temp.location, ISNULL (temp2.approved,0),temp.brgyindexfrom RPTLedgerleft join( select Realproperty.location[location], RPTLedger.objid[objid], brgyIndex[brgyindex] from RPTLedger join RealProperty on (RPTLedger.realPropertyId = RealProperty.objid) group by RealProperty.location, brgyIndex, RPTLedger.objid)tempon RPTLedger.objid=temp.objidleft join(select Realproperty.location[location], COUNT (tdno)[approved]from RPTLedgerjoin RealProperty on (RPTLedger.realPropertyId = RealProperty.objid)where RPTLedger.state='PENDING'group by RealProperty.location)temp2on temp.location=temp2.location),TotalApproved (location3, totalapproved, brgyindex)as(select distinct temp.location, ISNULL (temp2.approved,0),temp.brgyindexfrom RPTLedgerleft join( select Realproperty.location[location], RPTLedger.objid[objid], brgyIndex[brgyindex] from RPTLedger join RealProperty on (RPTLedger.realPropertyId = RealProperty.objid) group by RealProperty.location, brgyIndex, RPTLedger.objid)tempon RPTLedger.objid=temp.objidleft join(select Realproperty.location[location], COUNT (tdno)[approved]from RPTLedgerjoin RealProperty on (RPTLedger.realPropertyId = RealProperty.objid)where RPTLedger.state='APPROVED'group by RealProperty.location)temp2on temp.location=temp2.location), TotalExcempt (location4, totalexcempt, brgyindex)as(select temp3.location, ISNULL(temp4.expt,0)[excempt], temp3.brgyindex from RPTLedgerleft join( select Realproperty.location[location], RPTLedger.objid[objid], brgyIndex[brgyindex] from RPTLedger join RealProperty on (RPTLedger.realPropertyId = RealProperty.objid) group by RealProperty.location, brgyIndex, RPTLedger.objid)temp3 on RPTLedger.objid = temp3.objidleft join(select location, COUNT (RPTLedger.tdno)[expt]from RPTLedger join TaxDeclaration on (RPTLedger.tdid=TaxDeclaration.objid) join RPU on (RPU.objid = TaxDeclaration.rpuid)where exemptCode is not NULLand RPTLedger.state ='PENDING'group by location)temp4 on temp3.location=temp4.location),TotalZeroAss (location5, totalzero, brgyindex)as(select temp5.location, ISNULL(temp6.zero,0)[ZeroCount], temp5.brgyindex from RPTLedgerleft join( select Realproperty.location[location], RPTLedger.objid[objid], brgyIndex[brgyindex] from RPTLedger join RealProperty on (RPTLedger.realPropertyId = RealProperty.objid) group by RealProperty.location, brgyIndex, RPTLedger.objid)temp5 on RPTLedger.objid = temp5.objidleft join(select location, COUNT (RPTLedger.tdno)[zero]from RPTLedger join TaxDeclaration on (RPTLedger.tdid=TaxDeclaration.objid) join RPU on (RPU.objid = TaxDeclaration.rpuid)where exemptCode is NULLand RPTLedger.state ='PENDING'and RPTLedger.assessedValue ='0'group by location)temp6 on temp6.location=temp5.location)select TotalRecord.location1[Barangay], totalrecords,totalapproved,totalpending, totalexcempt, totalzerofrom TotalRecord join TotalPending on (TotalRecord.location1 =TotalPending.location2)join TotalApproved on (TotalRecord.location1=TotalApproved.location3)join TotalExcempt on (TotalRecord.location1 = TotalExcempt.location4)join TotalZeroAss on (TotalRecord.location1=TotalZeroAss.location5)group by TotalRecord.location1, totalrecords, TotalPending.location2,totalpending, TotalApproved.location3, totalapproved, TotalApproved.brgyindex, totalexcempt, totalzeroorder by TotalApproved.brgyindex asc
this query run for more than a minute around 1mins and 45sec top so i really need to make it faster if that is still possible :)any instructions or tip on how i can optimize this one will be a great help thanks in advance