即使一个结果不存在,多个select语句也会返回结果

时间:2014-03-03 14:47:06

标签: sql sql-server

所以我有一个像这样的多元选择语句。

问题是当其中一个子选择不存在时,如果不返回结果。

如果不存在,我怎么能这样做呢?

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

4 个答案:

答案 0 :(得分:2)

以下查询将您的多个连接更改为条件聚合。这将解决如果不满足其中一个条件的问题,那么您仍然会得到结果。如果任何查询没有行,则在cross join子句中执行from将导致无记录。

我还用其他几种方法修正了逻辑。现在,定义agentName的子查询已加入其余部分,因此不必在条件中重复AgentName。我还添加了group by,因此如果您愿意,可以一次只有AgentName

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;

答案 1 :(得分:0)

试试这个

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 

答案 2 :(得分:0)

为什么不使用SQL isNull语句替换没有找到数据的空字符串?

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

答案 3 :(得分:0)

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