对于Union All,SQL命令未正确结束

时间:2016-10-19 05:35:19

标签: sql oracle11g union-all

我无法理解代码中的错误。有人可以帮帮我吗?

这是我的SQL查询

SELECT S.SUBSTR(ASSIGNED_NUMBER, -4) FROM (
   SELECT SUBSTR(ASSIGNED_NUMBER, -4) AS SDATE FROM TBL_DATA1 GROUP BY SUBSTR(ASSIGNED_NUMBER, -4)
   UNION ALL
   SELECT SUBSTR(ASSIGNED_NUMBER, -4) AS SDATE FROM TBL_DATA2 GROUP BY SUBSTR(ASSIGNED_NUMBER, -4)
) AS S GROUP BY S.SUBSTR(ASSIGNED_NUMBER, -4);

Note: Originally, My outer column is S.SDATE and edited it to S.SUBSTR(ASSIGNED_NUMBER, -4)

输出:

ORA-00933: SQL command not properly ended 00933. 00000 - "SQL command not properly ended"

顺便说一下,ASSIGNED_NUMBER列中的示例日期为58-2016。 我使用substring来获得2016年

2 个答案:

答案 0 :(得分:3)

当您在子查询别名SDATE中给出列时,您正在使用S.SUBSTR(ASSIGNED_NUMBER,-4)。

此外,使用子查询时,命名应为) s而不是) AS S

您的查询应该是

SELECT S.SDATE FROM (
   SELECT SUBSTR(ASSIGNED_NUMBER, -4) AS SDATE FROM TBL_DATA1 GROUP BY SUBSTR(ASSIGNED_NUMBER, -4)
   UNION ALL
   SELECT SUBSTR(ASSIGNED_NUMBER, -4) AS SDATE FROM TBL_DATA2 GROUP BY SUBSTR(ASSIGNED_NUMBER, -4)
) S GROUP BY S.SDATE;

答案 1 :(得分:0)

SELECT S.SDATE FROM (
   SELECT SUBSTR(ASSIGNED_NUMBER, -4) AS SDATE FROM TBL_DATA1 GROUP BY SUBSTR(ASSIGNED_NUMBER, -4)
   UNION ALL
   SELECT SUBSTR(ASSIGNED_NUMBER, -4) AS SDATE FROM TBL_DATA2 GROUP BY SUBSTR(ASSIGNED_NUMBER, -4)
)  S GROUP BY S.SDATE;