Question

So I have a mulitple select statement that goes like this.

The problem is when one of the sub select does not exist if will not return a result.

How can I make it that if it does not exist it will be null.

SELECT agent,
       percentage1,
       percentage2,
       percentage3,
       percentage4,
       percentage5
FROM
  (SELECT 'Agent 01' AS agent) AS agent,    
  (SELECT percentage AS percentage1
   FROM APOS_QA_Scorecard..scorecard
   WHERE mon = 'November'
     AND dateMonitored = 4
     AND agentName = 'Agent 01') AS percentage1,    
  (SELECT percentage AS percentage2
   FROM APOS_QA_Scorecard..scorecard
   WHERE mon = 'November'
     AND dateMonitored = 5
     AND agentName = 'Agent 01') AS percentage2,    
  (SELECT percentage AS percentage3
   FROM APOS_QA_Scorecard..scorecard
   WHERE mon = 'November'
     AND dateMonitored = 6
     AND agentName = 'Agent 01') AS percentage3,    
  (SELECT percentage AS percentage4
   FROM APOS_QA_Scorecard..scorecard
   WHERE mon = 'November'
     AND dateMonitored = 7
     AND agentName = 'Agent 01') AS percentage4,    
  (SELECT percentage AS percentage5
   FROM APOS_QA_Scorecard..scorecard
   WHERE mon = 'November'
     AND dateMonitored = 8
     AND agentName = 'Agent 01') AS percentage5
Was it helpful?

Solution

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;

OTHER TIPS

Why not use the SQL isNull statement to substitute an empty string where no data is found?

select agent,percentage1,percentage2,percentage3,percentage4,percentage5 from 
(select 'Agent 01' as agent) as agent,
isnull((select percentage as percentage1 from APOS_QA_Scorecard..scorecard where mon = 'November' and dateMonitored = 4 and agentName = 'Agent 01' ),'') as percentage1,
isnull((select percentage as percentage2 from APOS_QA_Scorecard..scorecard where mon = 'November' and dateMonitored = 5 and agentName = 'Agent 01'),'') as percentage2,
isnull((select percentage as percentage3 from APOS_QA_Scorecard..scorecard where mon = 'November' and dateMonitored = 6 and agentName = 'Agent 01'),'') as percentage3,
isnull((select percentage as percentage4 from APOS_QA_Scorecard..scorecard where mon = 'November' and dateMonitored = 7 and agentName = 'Agent 01'),'') as percentage4,
isnull((select percentage as percentage5 from APOS_QA_Scorecard..scorecard where mon = 'November' and dateMonitored = 8 and agentName = 'Agent 01'),'') as percentage5

Try this instead

select agent,IsNull(p1.percentage,0.0) as Percentage1,
IsNull(p2.percentage,0.0) as Percentage2,
IsNull(p3.percentage,0.0) as Percentage3,
IsNull(p4.percentage,0.0) as Percentage4,
IsNull(p5.percentage,0.0) as Percentage5
from
(select 'Agent 01' as agent) as Agent
left join (select percentage as percentage 
           from APOS_QA_Scorecard..scorecard 
           where mon = 'November' and dateMonitored = 4) p1 on p1.agentName = agent.agent 
left join (select percentage as percentage 
           from APOS_QA_Scorecard..scorecard 
           where mon = 'November' and dateMonitored = 5) p2 on p2.agentName = agent.agent 
left join (select percentage as percentage 
           from APOS_QA_Scorecard..scorecard 
           where mon = 'November' and dateMonitored = 6) p3 on p3.agentName = agent.agent 
left join (select percentage as percentage 
           from APOS_QA_Scorecard..scorecard 
           where mon = 'November' and dateMonitored = 7) p4 on p4.agentName = agent.agent 
left join (select percentage as percentage 
           from APOS_QA_Scorecard..scorecard 
           where mon = 'November' and dateMonitored = 8) p5 on p5.agentName = agent.agent 
SELECT a.agent, 
  ap4.percentage AS percentage1, 
  ap5.percentage AS percentage2, 
  ap6.percentage AS percentage3, 
  ap7.percentage AS percentage4, 
  ap8.percentage AS percentage5 
FROM
  (SELECT 'Agent 01' AS agent) AS a
  LEFT JOIN APOS_QA_Scorecard..scorecard AS ap4 ON a.agent = ap4.agentName AND ap4.mon = 'November' and ap4.dateMonitored = 4 
  LEFT JOIN APOS_QA_Scorecard..scorecard AS ap5 ON a.agent = ap5.agentName AND ap5.mon = 'November' and ap5.dateMonitored = 5
  LEFT JOIN APOS_QA_Scorecard..scorecard AS ap6 ON a.agent = ap6.agentName AND ap6.mon = 'November' and ap6.dateMonitored = 6
  LEFT JOIN APOS_QA_Scorecard..scorecard AS ap7 ON a.agent = ap7.agentName AND ap7.mon = 'November' and ap7.dateMonitored = 7
  LEFT JOIN APOS_QA_Scorecard..scorecard AS ap8 ON a.agent = ap8.agentName AND ap8.mon = 'November' and ap8.dateMonitored = 8
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top