0

我写了以下脚本

SELECT vil.account_id , 
       vil.imp_id, 
       vil.owner ,
       vil.start_date,
       CASE  
         WHEN ac.status = 4  
         THEN status_date  
         ELSE NULL  
       END AS Live_date,
       cs.country_code  
  FROM ACCOUNT ac  
  INNER JOIN vu_imp_list vil  
     ON vil.account_id = ac.account_id  
  INNER JOIN entity e  
     ON ac.entity_id = e.entity_id  
  INNER JOIN country_specifics cs  
     ON e.country_id = cs.country_id  
  WHERE (vil.account_id, vil.start_date) =   
        (SELECT vils.account_id, MIN(vils.start_date)   
           FROM vu_imp_list vils  
          WHERE vil.account_id IN  ('825808009','987323136')  
          group by vils.account_id);  

但我一直收到错误

SQL Error: ORA-01427: single-row subquery returns more than one row  
01427. 00000 -  "single-row subquery returns more than one row"  

我试图为子查询添加rownum = 1子句where,但似乎查询永远运行。我已经单独测试了子查询,它为每个返回一行account_id。这里的大多数类似问题都与insert问题有关,update而不仅仅是查询。

4

2 回答 2

1

您需要确保子查询准确返回 1 行(或 0)。我想你想要这个:

  WHERE vil.start_date =   
        (SELECT MIN(vils.start_date)   
           FROM vu_imp_list vils  
          WHERE vils.account_id IN ('825808009','987323136')  
        ); 

或者IN如果子查询返回 2 行或更多行并且您想与start_date其中任何一个进行比较,则使用:

  WHERE vil.start_date IN   
        (SELECT MIN(vils.start_date)   
           FROM vu_imp_list vils  
          WHERE vils.account_id IN ('825808009','987323136')
          GROUP BY vils.account_id 
        ); 

或者这个,如果你想要一个相关的子查询:

  WHERE vil.account_id IN ('825808009','987323136')
    AND vil.start_date =   
        (SELECT MIN(vils.start_date)   
           FROM vu_imp_list vils  
          WHERE vils.account_id = vil.account_id 
        ); 
于 2013-10-23T06:30:43.877 回答
0

如果您的表vu_imp_list同时存在825808009并且987323136存在 for account_id,那么这个内部查询:

(SELECT vils.account_id, MIN(vils.start_date)   
 FROM vu_imp_list vils  
 WHERE vil.account_id IN ('825808009','987323136')  
 group by vils.ob10_account_id)

将生成 2 行,每列 2 列。

麻烦的是,那么您不能进行多列相等检查WHERE

WHERE (vil.account_id, vil.start_date) =   
    (SELECT vils.account_id, MIN(vils.start_date) ... )
于 2013-10-23T06:29:23.333 回答