问题描述
我们有一个查询在我们的 Prod 环境中失败,因为 ora-01427 单行子查询返回更多行。 这是oracle 11g 数据库。查询如下。此查询运行良好,直到我们使用 SQ3 添加最后的左外连接,一旦添加它在一段时间后失败并显示 ORA-1427。
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,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'
当我使用 WITH 子句重写此查询时,它运行良好,见下文。当下面的第二个查询在没有改变逻辑的情况下执行时,为什么第一个查询失败。
with
(SELECT disTINCT MAX(c1) c1,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 从 s1.t1 WHERE c2='NY' AND c7
GROUP BY c1
**
,您不需要按与聚合函数一起使用的列进行分组。因此,将您的最后一个查询更改为 -
SELECT MAX(c1) c1,c6
FROM s1.t1
WHERE c2 = 'NY'
AND c7 < '2'
AND c8 = 'Y'
GROUP BY c2,c6