单行子查询返回多行时的第一行

时间:2013-01-10 19:57:26

标签: oracle subquery

我的子查询返回多行,我需要第一行并保留我的最大函数

Select ...
Where GHTY_FIRME.FIRME_ID= (Select   FIRME_ID
                           FROM imag_verification_tube 
                           WHERE (numero_dossier =  '12004' OR NUMERO_TIRE= '12004')
                           AND CREE_LE = (select max (CREE_LE) 
                           from tableX where(numero_dossier ='12004' OR 
                           NUMERO_PQDCS= '12004 ')));

1 个答案:

答案 0 :(得分:0)

使用rownum伪列:

Select ...
Where GHTY_FIRME.FIRME_ID= (Select   FIRME_ID
                           FROM imag_verification_tube 
                           WHERE (numero_dossier =  '12004' OR NUMERO_TIRE= '12004')
                           AND CREE_LE = (select max (CREE_LE) 
                                          from tableX 
                                          where(numero_dossier ='12004' 
                                               OR NUMERO_PQDCS= '12004 ') 
                                               and rownum < 2));

另见On ROWNUM and Limiting Results

那就是说,你可以尝试优化sql。我要做的第一件事就是将内部SQL转换为连接。此外,根据您的业务逻辑, 的条件或条件也可以被删除。