I have a query which returns analytics style information about a job board and where applicants are coming from in the following structure:

The query to access that is:
SELECT g.name AS [Source]
,SUM(CASE WHEN v.cost = 0 THEN 1 ELSE 0 END) AS [Organic Clicks]
,SUM(CASE WHEN v.cost <> 0 THEN 1 ELSE 0 END) AS [Paid Clicks]
,COUNT(v.id) AS [Total Clicks]
,SUM(CASE WHEN a.applicant = 1 AND v.cost = 0 THEN 1 ELSE 0 END) AS [Organic Applicants]
,SUM(CASE WHEN a.applicant = 1 AND v.cost <> 0 THEN 1 ELSE 0 END) AS [Paid Applicants]
,SUM(CASE WHEN a.applicant = 1 THEN 1 ELSE 0 END) AS [Total Applicants]
,SUM(v.cost/100.0) AS [Spend]
FROM a_views v
LEFT OUTER JOIN a_views a
ON v.viewerid = a.viewerid
AND v.sessionsourceid = a.sessionsourceid
AND a.applicant = 1
JOIN a_sources s
ON v.sourceid = s.id
JOIN a_sourcegroups g ON s.fk_sourcegroup = g.id
--JOIN jobs j ON v.jobid = j.anal_id AND j.featured = 1
WHERE v.hostName = @jobboard
AND v.viewed_at >= @start AND v.viewed_at <= @end
GROUP BY g.name
The only issue is that in the LEFT OUTER JOIN a_views block there may be multiple records returned. What I need to do is only have the record tracked once in the Click sums but each time it’s found for the Applicant sums.
I did find a similar question of this happening on this question, but the answerer didn’t actually give much information.
To resum what I need, each instance of the record in the right side of the join, but only 1 instance of the record on the left side.
One way you could do this without changing your query entirely is separate the Click sums and Applicant sums into two separate queries,
UNIONthem together and then group/sum them again.Something like this (pseudo code):