ORDER BY upper (...) with UNION giving me problems

I am having trouble figuring out the reasons for this problem.

This code works exactly as it should. It joins two tables (MESSAGES and MESSAGES_ARCHIVE) and arranges them correctly.

SELECT * FROM ( 
    SELECT rownum as rn, a.* FROM ( 
        SELECT  
        outbound.FROM_ADDR, outbound.TO_ADDR, outbound.EMAIL_SUBJECT

        from MESSAGES outbound 
        where (1 = 1) 

        UNION ALL

        SELECT  
        outboundarch.FROM_ADDR, outboundarch.TO_ADDR, outboundarch.EMAIL_SUBJECT

        from MESSAGES_ARCHIVE outboundarch 
        where (1 = 1)  

        order by FROM_ADDR DESC 
    ) a 
) where rn between 1 and 25

However, this code does not work.

SELECT * FROM ( 
    SELECT rownum as rn, a.* FROM ( 
        SELECT  
        outbound.FROM_ADDR, outbound.TO_ADDR, outbound.EMAIL_SUBJECT

        from MESSAGES outbound 
        where (1 = 1) 

        UNION ALL

        SELECT  
        outboundarch.FROM_ADDR, outboundarch.TO_ADDR, outboundarch.EMAIL_SUBJECT

        from MESSAGES_ARCHIVE outboundarch 
        where (1 = 1)  

        order by upper(FROM_ADDR) DESC 
    ) a 
) where rn between 1 and 25

and returns this error

ORA-01785: ORDER BY item must be the number of a SELECT-list expression
01785. 00000 -  "ORDER BY item must be the number of a SELECT-list expression"

I am trying to get two tables ordered regardless of the case with the letter, so I use upper(FROM_ADDR). Any suggestions? Thank!

+4
source share
2 answers

, , , , union. , row_number():

SELECT * FROM ( 
    SELECT row_number() over (order by upper(FROM_ADDR)) as rn, a.*
    FROM ( 
        SELECT  
        outbound.FROM_ADDR, outbound.TO_ADDR, outbound.EMAIL_SUBJECT

        from MESSAGES outbound 
        where (1 = 1) 

        UNION ALL

        SELECT  
        outboundarch.FROM_ADDR, outboundarch.TO_ADDR, outboundarch.EMAIL_SUBJECT

        from MESSAGES_ARCHIVE outboundarch 
        where (1 = 1)  

    ) a 
)
where rn between 1 and 25
+1

upper() , . :

order by upper(FROM_ADDR) DESC

:

order by upper(FROM_ADDR) as FROM_ADDR DESC
0

Source: https://habr.com/ru/post/1544383/


All Articles