如何使用联合 (ORA-00904) 对 sql 查询进行排序

how to order sql query with union ( ORA-00904)

我有很多查询,我必须按 desc 订购,但我做不到。我认为工会有问题,但我想不通。

查询在不使用 ORDER BY 的情况下工作。

这里是查询:

SELECT T.EMPLOYEE_NUMBER,
       F.REG_ID,
       F.DURATION_NETLINE,
       F.FLIGHT_NUMBER,
       F.DEPARTURE_DATE,
       USER_LIFUS.NAME,
       USER_LIFUS.SURNAME,
       F.FORM_RESULT_STATUS,
       T.LIEX_NUMBER,
       F.IS_PC_FLIGHT,
       F.IS_DC_FLIGHT,
       FT.NAME FLIGHT_TYPE,
       T.IS_RFLC,
       F.IS_DC_FLIGHT,
       F.LEG_ID
  FROM TRAINEE T
INNER JOIN FLIGHT F
    ON F.EVALUATED_EMP_NUM = T.EMPLOYEE_NUMBER
   AND F.DEPARTURE_DATE >= T.LIFUS_START_DATE
   AND (F.IS_SUBMITTED = 1 AND F.IS_DELETED = 0 AND F.IS_CANCELLED = 0)
   AND F.IS_DC_FLIGHT = 0
   AND F.IS_FC_FLIGHT = 0
   --and f.leg_id <>0
INNER JOIN USER_LIFUS
    ON USER_LIFUS.EMPLOYEE_NUMBER = F.INSTRUCTOR_EMP_NUM
INNER JOIN FLIGHT_TYPE FT
    ON F.FLIGHT_TYPE_ID = FT.ID
WHERE T.EMPLOYEE_NUMBER = '088416'
UNION ALL
SELECT T.EMPLOYEE_NUMBER,F.REG_ID,
       F.DURATION_NETLINE,
       F.FLIGHT_NUMBER,
       F.DEPARTURE_DATE,
       USER_LIFUS.NAME,
       USER_LIFUS.SURNAME,
       F.FORM_RESULT_STATUS,
       T.LIEX_NUMBER,
       F.IS_PC_FLIGHT,
       F.IS_DC_FLIGHT,
       FT.NAME FLIGHT_TYPE,
       T.IS_RFLC,
       F.IS_DC_FLIGHT,
       F.LEG_ID
  FROM TRAINEE T
INNER JOIN FLIGHT F
    ON F.EVALUATED_EMP_NUM = T.EMPLOYEE_NUMBER
   AND F.DEPARTURE_DATE >= T.LIFUS_START_DATE
   AND (F.IS_SUBMITTED = 1 AND F.IS_DELETED = 0 AND F.IS_CANCELLED = 0)
   AND F.IS_DC_FLIGHT = 1
   and f.is_fc_flight = 0
   AND F.LEG_ID <> 0
INNER JOIN USER_LIFUS
    ON USER_LIFUS.EMPLOYEE_NUMBER = F.INSTRUCTOR_EMP_NUM
INNER JOIN FLIGHT_TYPE FT
    ON F.FLIGHT_TYPE_ID = FT.ID
WHERE T.EMPLOYEE_NUMBER = '123456'

当我添加

ORDER BY F.FLIGHT_NUMBER DESC

它给了我

ora-00904

假设您只添加了

ORDER BY F.FLIGHT_NUMBER DESC

在查询的底部,您需要将其添加到两个联合查询之外。

SELECT * FROM (
SELECT T.EMPLOYEE_NUMBER,
F.REG_ID,
       F.DURATION_NETLINE,
       F.FLIGHT_NUMBER,
       F.DEPARTURE_DATE,
       USER_LIFUS.NAME,
       USER_LIFUS.SURNAME,
       F.FORM_RESULT_STATUS,
       T.LIEX_NUMBER,
       F.IS_PC_FLIGHT,
       F.IS_DC_FLIGHT,
       FT.NAME FLIGHT_TYPE,
       T.IS_RFLC,
       F.IS_DC_FLIGHT,
       F.LEG_ID
  FROM TRAINEE T
INNER JOIN FLIGHT F
    ON F.EVALUATED_EMP_NUM = T.EMPLOYEE_NUMBER
   AND F.DEPARTURE_DATE >= T.LIFUS_START_DATE
   AND (F.IS_SUBMITTED = 1 AND F.IS_DELETED = 0 AND F.IS_CANCELLED = 0)
   AND F.IS_DC_FLIGHT = 0
   AND F.IS_FC_FLIGHT = 0
   --and f.leg_id <>0
INNER JOIN USER_LIFUS
    ON USER_LIFUS.EMPLOYEE_NUMBER = F.INSTRUCTOR_EMP_NUM
INNER JOIN FLIGHT_TYPE FT
    ON F.FLIGHT_TYPE_ID = FT.ID
WHERE T.EMPLOYEE_NUMBER = '088416'
UNION ALL
SELECT T.EMPLOYEE_NUMBER,F.REG_ID,
       F.DURATION_NETLINE,
       F.FLIGHT_NUMBER,
       F.DEPARTURE_DATE,
       USER_LIFUS.NAME,
       USER_LIFUS.SURNAME,
       F.FORM_RESULT_STATUS,
       T.LIEX_NUMBER,
       F.IS_PC_FLIGHT,
       F.IS_DC_FLIGHT,
       FT.NAME FLIGHT_TYPE,
       T.IS_RFLC,
       F.IS_DC_FLIGHT,
       F.LEG_ID
  FROM TRAINEE T
INNER JOIN FLIGHT F
    ON F.EVALUATED_EMP_NUM = T.EMPLOYEE_NUMBER
   AND F.DEPARTURE_DATE >= T.LIFUS_START_DATE
   AND (F.IS_SUBMITTED = 1 AND F.IS_DELETED = 0 AND F.IS_CANCELLED = 0)
   AND F.IS_DC_FLIGHT = 1
   and f.is_fc_flight = 0
   AND F.LEG_ID <> 0
INNER JOIN USER_LIFUS
    ON USER_LIFUS.EMPLOYEE_NUMBER = F.INSTRUCTOR_EMP_NUM
INNER JOIN FLIGHT_TYPE FT
    ON F.FLIGHT_TYPE_ID = FT.ID
WHERE T.EMPLOYEE_NUMBER = '123456')
ORDER BY FLIGHT_NUMBER DESC

这样做:

select temp.T.Employee_number,temp.F.REG_ID,temp.F.FLIGHT_NUMBER....
from (  --your query mentioned above-- ) temp
order by temp.F.FLIGHT_NUMBER desc;