简体   繁体   中英

ORA-1427 in inline view

We have a query that fails in our Prod environment that fails with ora-01427 single-row subquery returns more rows. This is oracle 11g database. Query as below. This query runs fine till we add the final left outer join with SQ3, once added it fails with ORA-1427 after some time.

select c1,c2..c8 from
t1 left join
(subquery with joins)SQ1
left join
(subquery with joins)SQ2
left join
(subquery with joins)SQ4
left join
(subquery with joins)SQ5
left join
(SELECT DISTINCT MAX(c1) c1, c2, c3, c4, c5,c6 
     FROM s1.t1 WHERE  c2='NY' AND c7<'2' AND c8='Y' 
GROUP BY c1, c2, c3, c4, c5,c6) SQ3 ON sq3.c3=t1.c3
                                                 AND sq3.c8=t1.c8
                                                  AND sq3.c7=t2.c6
                                                  AND sq3.c6 <'2'
                                                AND sq3.c4='Y' 

When i rewrite this query using WITH clause then it runs fine, see below. Any idea on why the first query fails when the second one below executes with no change to logic.

with
(SELECT DISTINCT MAX(c1) c1, c2, c3, c4, c5,c6 
     FROM s1.t1 WHERE  c2='NY' AND c7<'2' AND c8='Y' 
GROUP BY c1, c2, c3, c4, c5,c6) as SQ3
select c1,c2..c8 from
t1 left join
(subquery with joins)SQ1
left join
(subquery with joins)SQ2
left join
(subquery with joins)SQ4
left join
(subquery with joins)SQ5
left join
 SQ3 ON sq3.c3=t1.c3
 AND sq3.c8=t1.c8
 AND sq3.c7=t2.c6
 AND sq3.c6 <'2'
 AND sq3.c4='Y' 

Mira a ver esto de agrupar por la columna del agregado, no parece correcto (SELECT DISTINCT MAX(c1) c1 , c2, c3, c4, c5,c6 FROM s1.t1 WHERE c2='NY' AND c7<'2' AND c8='Y' **

GROUP BY c1

**

You don't need to group by the column you are using with your aggregate function. So change your last query to -

SELECT MAX(c1) c1, c2, c3, c4, c5,c6 
  FROM s1.t1
 WHERE c2 = 'NY'
   AND c7 < '2'
   AND c8 = 'Y' 
 GROUP BY c2, c3, c4, c5,c6

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM