我需要编写一个加入大量查询的查询。它们通过ID连接。问题是在其中一个查询中,我必须选择的值是“单组组函数”,我必须评估必须使用第二个选定列的ON
子句的条件,即我我无法添加。我会写一个比我的代码更简单的例子:
Select frst.FirstResult, scnd.FirstResult
from(
Select something AS FirstResult, ID_to_compare as SecondResult from table
Where -- [...]
) frst
join(
Select something AS FirstResult, ID_to_compare as SecondResult from table2
Where -- [...]
) scnd
ON frst.SecondResult=scnd.SecondResult
join(
Select something AS FirstResult, ID_to_compare as SecondResult from table3
Where -- [...]
) trd
ON scnd.SecondResult=trd.SecondResult
-- [...]
join(
Select single_group_function(params) AS FirstResult, ID_to_compare as SecondResult from table3
--This select cannot be done because of the group function cannot be executed
Where -- [...]
) trd
ON svn.SecondResult=eit.SecondResult
我的问题是我需要比较每个选择的“SecondResult”,但是不允许成员组进行这样的查询。我试过“双”表,但它对我来说真的很乱,我不明白如何使用它。我也尝试过在宏查询中加入它们,但是每一个选择都足够大,真的很难写。我已经从this post中获取了很多想法。你有任何暗示要完成我的任务吗?
对于MT0的请愿,最小完整且可验证的问题是:
Select frst.FirstField, scnd.FirstField, frst.SecondField
from
(Select a1.DUMMY as FirstField, a2.DUMMY as SecondField
from dual a1 join dual a2 on 1=1) frst
join
(Select a1.DUMMY as FirstField, a2.DUMMY as SecondField
from dual a1 join dual a2 on 1=1) scnd
ON frst.SecondField = scnd.SecondField
join
(Select sum(a1.DUMMY) as FirstField, a2.DUMMY as SecondField
from dual a1 join dual a2 on 1=1) trd
ON trd.SecondField = scnd.SecondField
;
我希望在这种情况下
frst.FirstField scnd.FirstField frst.SecondField
---------------- ---------------- --------------------
X X 1
但我得到了
Error en la línea de comandos : 11 Columna : 40
Informe de error -
Error SQL: ORA-00937: la función de grupo no es de grupo único
00937. 00000 - "not a single-group group function"
*Cause:
*Action:
答案 0 :(得分:2)
这是您的示例子查询:
Select sum(a1.DUMMY) as FirstField, a2.DUMMY as SecondField
from dual a1 join dual a2 on 1=1
那么你期望一个结果行吗?这是没有GROUP BY
的聚合。您可以获得所有记录的总和,但是在此结果记录中显示哪个a2.DUMMY
?这就是DBMS所抱怨的。
您可能想要的是每a2.DUMMY
的总和。因此,请按此列分组:
Select sum(a1.DUMMY) as FirstField, a2.DUMMY as SecondField
from dual a1 join dual a2 on 1=1
GROUP BY a2.DUMMY
答案 1 :(得分:2)
在您所做的简化示例中:
join
(Select sum(a1.DUMMY) as FirstField, a2.DUMMY as SecondField
from dual a1 join dual a2 on 1=1) trd
ON trd.SecondField = scnd.SecondField
您可以进一步简化以查看问题:
Select sum(a1.DUMMY) as FirstField, a2.DUMMY as SecondField
from dual a1 join dual a2 on 1=1;
ORA-00937: not a single-group group function
您必须在group-by子句中包含 not 聚合的任何列:
Select sum(a1.DUMMY) as FirstField, a2.DUMMY as SecondField
from dual a1 join dual a2 on 1=1
group by a2.DUMMY;
现在将获得ORA-01722,因为dummy是一个字符串;使用max代替:
Select frst.FirstField, scnd.FirstField, frst.SecondField
from
(Select a1.DUMMY as FirstField, a2.DUMMY as SecondField
from dual a1 join dual a2 on 1=1) frst
join
(Select a1.DUMMY as FirstField, a2.DUMMY as SecondField
from dual a1 join dual a2 on 1=1) scnd
ON frst.SecondField = scnd.SecondField
join
(Select max(a1.DUMMY) as FirstField, a2.DUMMY as SecondField
from dual a1 join dual a2 on 1=1 group by a2.DUMMY) trd
ON trd.SecondField = scnd.SecondField
;
F F S
- - -
X X X
所以你真正的查询需要做类似的事情:
Select single_group_function(params) AS FirstResult, ID_to_compare as SecondResult from table3
Where -- [...]
group by ID_to_compare
在子查询中