使用GROUP / ORDER BY的两个查询的UNION导致ORA-00933:SQL命令未正确结束

时间:2014-10-29 14:33:30

标签: sql oracle group-by sql-order-by union

我正在尝试使用UNION子句将两个SQL查询合并为一个。但是,我在某种程度上坚持使用UNION运算符行上的错误消息ORA-00933: SQL command not properly ended

两个查询之间的唯一区别是在其中一个语句的WHERE子句中进行了额外的检查,并且查询了不同的b.typeOf值。其余的100%完全相同。

我真的不明白为什么Oracle希望SQL命令在UNION运算符之前结束,我的意思是这些运算符存在的唯一原因是组合查询...为什么它会在组合之前结束查询它带有以下声明:/

我唯一能想到的是它在语句中遇到GROUP BY和/或ORDER BY子句的问题。但是,这些是至关重要的,所以我不能删除它们并再次尝试,因为没有它,数据就没有任何意义。

是否有人知道此行为的解释以及可能的解决方法?试图在SO或谷歌上找到类似的问题但不幸的是,收益率并不高。任何提示都表示赞赏。

这是我的SQL:

SELECT 'Test' as Name,
extract(Year FROM a.datum) AS Jahr, extract(Month FROM a.datum) AS Monat, count(a.datum) AS  Anzahl_Tickets 
FROM table1 a, table2 b, table2 c 
WHERE a.fk_ID = b.id and b.fk_ID = c.id and b.typeOf in (3,4) 
and (a.isXYZ = 0 or a.isXYZ is Null or a.ZYX > 0) 
and mod(a.fk_id,2) = 1
and ...
and ...
and ... 
GROUP BY extract(Year FROM a.datum), extract(Month FROM a.datum)
ORDER BY extract(Year FROM a.datum), extract(Month FROM a.datum)

UNION

SELECT 'Test' as Name, 
extract(Year FROM a.datum) AS Jahr, extract(Month FROM a.datum) AS Monat, count(a.datum) AS Anzahl_Tickets 
FROM table1 a, table2 b, table2 c 
WHERE a.fk_ID = b.id and b.fk_ID = c.id and b.typeOf in (0,1) 
and (a.isXYZ = 0 or a.isXYZ is Null or a.ZYX > 0)
-- Notice missing modulo calculation. Thats basically the whole point of splitting this into 2 separate queries.
and ...
and ...
and ... 
GROUP BY extract(Year FROM a.datum), extract(Month FROM a.datum) 
ORDER BY extract(Year FROM a.datum), extract(Month FROM a.datum);  

编辑:现在UNION工作的一个新问题。它没有像我预期的那样显示结果:

http://i.imgur.com/cBQpbFv.png

而不是4列我实际上已经开始2.为什么它不将371 + 2和1 + 401加到一个数据集中?

编辑2:没关系,通过将外部子句更改为:

来修复它
SELECT Name, JAHR, MONAT, SUM(Anzahl_Tickets) FROM 
(
....
) GROUP BY Name, Jahr, Monat ORDER BY Jahr, Monat;

2 个答案:

答案 0 :(得分:2)

我建议将order by放在子查询之外(并将UNION更改为UNION ALL),如下所示:

select * from
(SELECT 'Test' as Name,
        extract(Year FROM a.datum) AS Jahr, 
        extract(Month FROM a.datum) AS Monat, 
        count(a.datum) AS  Anzahl_Tickets 
 FROM table1 a, table2 b, table2 c 
 WHERE a.fk_ID = b.id and b.fk_ID = c.id and b.typeOf in (3,4) 
 and (a.isXYZ = 0 or a.isXYZ is Null or a.ZYX > 0) 
 and mod(a.fk_id,2) = 1
 and ...
 and ...
 and ... 
 GROUP BY extract(Year FROM a.datum), extract(Month FROM a.datum)
 UNION ALL
 SELECT 'Test' as Name, 
        extract(Year FROM a.datum) AS Jahr, 
        extract(Month FROM a.datum) AS Monat, 
        count(a.datum) AS Anzahl_Tickets 
 FROM table1 a, table2 b, table2 c 
 WHERE a.fk_ID = b.id and b.fk_ID = c.id and b.typeOf in (0,1) 
 and (a.isXYZ = 0 or a.isXYZ is Null or a.ZYX > 0)
 -- Notice missing modulo calculation. Thats basically the whole point of splitting this into 2 separate queries.
 and ...
 and ...
 and ... 
 GROUP BY extract(Year FROM a.datum), extract(Month FROM a.datum) 
) ORDER BY Jahr, Monat; 

答案 1 :(得分:0)

从第一个子查询中删除ORDER BY子句,或者为了在两个子查询中保存排序,请使用以下内容:

with t1 as (select 2 a from dual union all select 1 from dual),
     t2 as (select 4 a from dual union all select 3 from dual)

select * from (select * from t1 order by a)
union all 
select * from (select * from t2 order by a);