UNION Statement Same Selected Values - Strange Behavior
本问题已经有最佳答案,请猛点这里访问。
我发现在具有相同值的
试图在网上找到一些事情,但不知道如何用短语搜索这个问题,需要解释,具体如下:
测试1:
1 2 3 4 5 6 7 8 9 10 | SELECT ID, SUM(AMT) AS AMT FROM ( SELECT 1 AS ID, 100 AS AMT FROM DUAL UNION SELECT 1 AS ID, 100 AS AMT FROM DUAL UNION SELECT 1 AS ID, 100 AS AMT FROM DUAL UNION SELECT 1 AS ID, 100 AS AMT FROM DUAL ) GROUP BY ID; |
测试2:
1 2 3 4 | SELECT 1 AS ID, 100 AS AMT FROM DUAL UNION SELECT 1 AS ID, 100 AS AMT FROM DUAL UNION SELECT 1 AS ID, 100 AS AMT FROM DUAL UNION SELECT 1 AS ID, 100 AS AMT FROM DUAL; |
号
所有这些select语句都给出了相同的结果:
。
所以,假设我正在编写一个动态代码,在运行时在我的一个应用程序中生成和执行,而代码恰好如上所述!如何使此select语句工作?
请在下面试试
1 2 3 4 5 6 7 | SELECT 1 AS ID, 100 AS AMT FROM DUAL UNION ALL SELECT 1 AS ID, 100 AS AMT FROM DUAL UNION ALL SELECT 1 AS ID, 100 AS AMT FROM DUAL UNION ALL SELECT 1 AS ID, 100 AS AMT FROM DUAL; |
号
你可以试着用union all
1 2 3 4 5 6 7 8 9 10 | SELECT ID, SUM(AMT) AS AMT FROM ( SELECT 1 AS ID, 100 AS AMT FROM DUAL UNION ALL SELECT 1 AS ID, 100 AS AMT FROM DUAL UNION ALL SELECT 1 AS ID, 100 AS AMT FROM DUAL UNION ALL SELECT 1 AS ID, 100 AS AMT FROM DUAL ) GROUP BY ID; |