Sorry, but you are guessing at a solution. And you are probably a bit stressed right now. Those two things are a good way to turn out an incorrect report.
Distinct is rarely a solution.
You need the number of applications that were funded last calendar year, for each dealer, correct?
Try this.
Write the query that gives you the count of funded applications, by dealer, for last year. Use GROUP BY.
Left join that subquery to the rest of the query.
Add the field that I called "FundedLastYear_Count" to the SELECT in the original query.
I may have the join and group by conditions incorrect, but I'll bet it works or it's darn close.
Attaching the schema and some sample data, like I reference in my signature, would have made this a lot easier.
Like this:
SELECT
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
,C2.nameshort
--,(SELECT nameshort FROM channels WHERE channelid = c.assignedbranch) AS Facilitator
,C.DateLastReview
,HC.HoldingCoDescription
--Funded, Approved,Conditioned, Declined
FROM Channels AS C
INNER JOIN ChannelContacts AS CC ON C.ChannelID = CC.ChannelID
INNER JOIN ChannelProductPlan AS CPP ON C.ChannelID = CPP.ChannelID
INNER JOIN tblLuMktReps AS MR ON C.MarketRepID = MR.MarketRepID
INNER JOIN tblLuHoldingCo AS HC ON C.HoldingCoID = HC.HoldingCoID
INNER JOIN channels C2 ON c.assignedbranch = C2.channelid
LEFT JOIN (SELECT
Count(C2.AppID as FundedLastYear_Count,
C.OldDealerID
FROM Channels c
LEFT JOIN Applications a ON c.ChannelID = a.SourceID
LEFT JOIN Contracts2 c2 ON a.AppID = c2.AppID
WHERE c2.DateContractFunded >= dateadd(yy, - 1, DATEADD(yy, DATEDIFF(yy, 0, getdate()), 0))
AND c2.DateContractFunded < DATEADD(yy, DATEDIFF(yy, 0, getdate()), 0)
GROUP BY C.OldDealerID) AS FundedLastYear ON C.OldDealerID = FundedLastYear.OldDealerID
WHERE
C.ChannelType IN (1, 6)
AND C.OldDealerID <> 1234
AND C.ActiveFlag = 'Y'
AND CC.Relationship = 1
ORDER BY C.OldDealerID
Michael L John
If you assassinate a DBA, would you pull a trigger?
To properly post on a forum:
http://www.sqlservercentral.com/articles/61537/