Can anyone look and comment how can I make the following query more efficient especially the subqueriesSELECT DISTINCT C.OldDealerID,C.NameLong,C.NameShort,C.MailingState,CC.NameFirst,CC.NameLast,CC.Phone,CC.FaxPhone,MR.Name,CC.Address,CC.City,CC.Zip,C.MailingStreet ,C.MailingCity ,CC.EMailAddress---,(SELECT NameShort FROM Channels C WHERE C.ChannelID = C.AssignedBranch) AS Facilitator--,(select NameShort from Channels C where C.channelID = C.assignedbranch) as Facilitator,C.DateLastReview,HC.HoldingCoDescription---Funded, Approved,Conditioned, Declined,(Select Count(C2.AppID) From Channels c left join Applications a on c.ChannelID = a.SourceID left join Contracts2 c2 on a.AppID = c2.AppID Where C.ChannelID = c.ChannelID and c2.DateContractFunded > (Select dateadd(yy,-1,DATEADD(yy, DATEDIFF(yy,0,getdate() ), 0))) and c2.DateContractFunded < (Select dateadd(ms,-3,DATEADD(yy, DATEDIFF(yy,0,getdate() ), 0))) ) As FundedLastYear,(Select Count(ad.AppID) From Channels c left join Applications a on c.ChannelID = a.SourceID left join AppDetails ad on a.AppID = ad.AppID Where C.ChannelID = c.ChannelID and StatusAppDetail = 'A' and DateDetailDisposed > (Select dateadd(yy,-1,DATEADD(yy, DATEDIFF(yy,0,getdate() ), 0))) and DateDetailDisposed < (Select dateadd(ms,-3,DATEADD(yy, DATEDIFF(yy,0,getdate() ), 0)))) As ApprovedLastYear,(Select Count(ad.AppID) From Channels c left join Applications a on c.ChannelID = a.SourceID left join AppDetails ad on a.AppID = ad.AppID Where C.ChannelID = c.ChannelID and StatusAppDetail ='I' and DateDetailDisposed > (Select dateadd(yy,-1,DATEADD(yy, DATEDIFF(yy,0,getdate() ), 0))) and DateDetailDisposed < (Select dateadd(ms,-3,DATEADD(yy, DATEDIFF(yy,0,getdate() ), 0)))) As ConditionedLastYear,(Select Count(ad.AppID) From Channels c left join Applications a on c.ChannelID = a.SourceID left join AppDetails ad on a.AppID = ad.AppID Where C.ChannelID = c.ChannelID and StatusAppDetail = 'D' and DateDetailDisposed > (Select dateadd(yy,-1,DATEADD(yy, DATEDIFF(yy,0,getdate() ), 0))) and DateDetailDisposed < (Select dateadd(ms,-3,DATEADD(yy, DATEDIFF(yy,0,getdate() ), 0)))) As DeclinedLastYear,(Select Count(C2.AppID) From Channels c left join Applications a on c.ChannelID = a.SourceID left join Contracts2 c2 on a.AppID = c2.AppID Where C.ChannelID = c.ChannelID and c2.DateContractFunded > (Select dateadd(ms,-3,DATEADD(yy, DATEDIFF(yy,0,getdate() ), 0))) ) As FundedThisYear,(Select Count(ad.AppID) From Channels c left join Applications a on c.ChannelID = a.SourceID left join AppDetails ad on a.AppID = ad.AppID Where C.ChannelID = c.ChannelID and StatusAppDetail = 'A' and DateDetailDisposed > (Select dateadd(ms,-3,DATEADD(yy, DATEDIFF(yy,0,getdate() ), 0)))) As ApprovedThisYear,(Select Count(ad.AppID) From Channels c left join Applications a on c.ChannelID = a.SourceID left join AppDetails ad on a.AppID = ad.AppID Where C.ChannelID = c.ChannelID and StatusAppDetail ='I' and DateDetailDisposed > (Select dateadd(ms,-3,DATEADD(yy, DATEDIFF(yy,0,getdate() ), 0)))) As ConditionedThisYear,(Select Count(ad.AppID) From Channels c left join Applications a on c.ChannelID = a.SourceID left join AppDetails ad on a.AppID = ad.AppID Where C.ChannelID = c.ChannelID and StatusAppDetail = 'D' and DateDetailDisposed > (Select dateadd(ms,-3,DATEADD(yy, DATEDIFF(yy,0,getdate() ), 0)))) As DeclinedThisYear From Channels AS C,ChannelProductPlan AS CPP,ChannelContacts AS CC,tblLuMktReps AS MR,tblLuHoldingCo AS HCWhere ActiveFlag = 'Y'and C.ChannelID = CPP.ChannelID and C.ChannelID = CC.ChannelIDand Relationship = 1 and C.MarketRepID = MR.MarketRepIDand C.OldDealerID <> 1234and Channeltype = 1and C.HoldingCoID = HC.HoldingCoIDor ActiveFlag = 'Y' and C.ChannelID = CPP.ChannelIDand C.ChannelID = CC.ChannelIDand Relationship = 1 and C.MarketRepID = MR.MarketRepIDand C.OldDealerID <> 1234and Channeltype = 6 and C.HoldingCoID = HC.HoldingCoIDOrder By MR.Name, C.MailingState, C.NameLong
↧