DECLARE @brand_did INT

SET @brand_did = 101785

SELECT

r2.parameter_desc AS Outcome

, COUNT(CASE WHEN i.portal_status_did = '104' THEN 1 END) [Status:Closed]

, COUNT(CASE WHEN i.portal_status_did = '101502' THEN 1 END) [Status:New]

, COUNT(CASE WHEN i.portal_status_did = '104904' THEN 1 END) [Status:Accepted]

, COUNT(CASE WHEN i.source_did = '101142' THEN 1 END) [Source:Campaign]

, COUNT(CASE WHEN i.source_did = '114' THEN 1 END) [Source:E-mail]

, COUNT(CASE WHEN i.source_did = '105003' THEN 1 END) [Source:Facebook]

, COUNT(CASE WHEN i.source_did = '101908' THEN 1 END) [Source:Microsite]

, COUNT(CASE WHEN i.source_did = '104816' THEN 1 END) [Source:Mobile Device]

, COUNT(CASE WHEN i.source_did = '101911' THEN 1 END) [Source:Motorshow]

, COUNT(CASE WHEN i.source_did = '132' THEN 1 END) [Source:Phone]

, COUNT(CASE WHEN i.source_did = '101906' THEN 1 END) [Source:Skoda Website]

, COUNT(CASE WHEN i.source_did = '101909' THEN 1 END) [Source:Dealer]

, COUNT(*) AS Total

FROM incident i (NOLOCK)

LEFT

OUTER

JOIN reference_parameter_ml r2 (NOLOCK)

ON r2.reference_parameter_did = i.portal_outcome_did

WHERE i.vehicle_purpose_did = @brand_did

GROUP

BY r2.parameter_desc