The following query changes your multiple joins to conditional aggregations. This will fix the problem that if one of the conditions is not met, then you will still get results. Doing a cross join
in your from
clause will result in no record if any of the queries have no rows.
I also fixed the logic in a couple of other ways. The subquery to define your agentName
is now joined to the rest, so the AgentName
doesn't have to be repeated in the conditions. I also added a group by
, so you can have more than on AgentName
at a time, if you like:
select a.agentName,
sum(case when mon = 'November' and dateMonitored = 4
then percentage
end) as percentage1,
sum(case when mon = 'November' and dateMonitored = 5
then percentage
end) as percentage2,
sum(case when mon = 'November' and dateMonitored = 6
then percentage
end) as percentage3,
sum(case when mon = 'November' and dateMonitored = 7
then percentage
end) as percentage4,
sum(case when mon = 'November' and dateMonitored = 8
then percentage
end) as percentage5
from (select 'Agent 01' as agentName
) a left outer join
APOS_QA_Scorecard..scorecard sc
on a.agentName = sc.agentName
group by a.agentName;