单行子查询返回多行。这里出了什么问题?

时间:2016-04-12 05:53:45

标签: sql oracle

with choice  as 
   (select level as dow,trunc(sysdate ,'D') - level as days
      from dual
      connect by level <=10
   ) 
select 
   (select count(*) from man,choice 
     where man_status='ACCEPTED'
     and  man_date_pub=choice.days 
     group by man_date_pub) as accepted,
   (select count(*) from man,choice 
     where man_status='SUBMITTED' 
     and man_date_sub=choice.days 
     group by man_date_sub) as submitted,
   (select count(*) from man,choice
     where man_status='CREATED' 
     and man_date_created=choice.days
     group by man_date_created) as created,
     choice.days
from choice 
order by days;

这给了我这个错误:

  

ORA-01427:单行子查询返回多行   01427. 00000 - &#34;单行子查询返回多行&#34;

2 个答案:

答案 0 :(得分:1)

标量游标必须返回一行。您的子查询不会这样做,因为多天都有点击。这种方法可以解决它:

with choice as 
            (select trunc(sysdate, 'D' ) - level as days
                from dual
                connect by level <=10
         ) 
select 
      sum(case when man_status='ACCEPTED' and man_date_pub=choice.days then 1 else 0 end)  as accepted,
      sum(case when man_status='SUBMITTED' and man_date_sub=choice.days then 1 else 0 end)  as submitted,
      sum(case when man_status='CREATED' and man_date_created=choice.days then 1 else 0 end)  as created,
      choice.days
from choice
      left join man
         on (man_date_pub = choice.days
             or man_date_sub = choice.days
             or man_date_created = choice.days)
group by choice.days
order by choice.days
/

答案 1 :(得分:0)

可能导致你的group by子句尝试删除group by子句并检查 喜欢: with choice as (select level as dow,trunc(sysdate ,'D') - level as days from dual connect by level <=10 ) select (select count(*) from man,choice where man_status='ACCEPTED' and man_date_pub=choice.days) as accepted, (select count(*) from man,choice where man_status='SUBMITTED' and man_date_sub=choice.days) as submitted, (select count(*) from man,choice where man_status='CREATED' and man_date_created=choice.days) as created, choice.days from choice order by days;