Skip to Main Content

SQL & PL/SQL

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

How to order first set in a UNION?

Hello everyone, i'm using oracle 21C xe.

I want list the total of cases of wine sold by wine types in alphabetical order, and them have a grand total cases sold for all wine.

I use this code which unions a GROUP BY query with a query that generates the total, I can not put ORDER BY after the first set, which causes error, so I put at the end.

SELECT wine, sum(cases_sold) FROM ws
join wines
on ws.wine_id = wines.wine_id
group by wine

union

select 'Total', sum(cases_sold) from ws
order by wine;

The result output is thet Total is not the final row because there is a wine (Vilana) that ranked alphabetically after Total.

How can write a query that would ensure that Total query row is the last row while keep first set in an alphabetical order?

thanks.

This post has been answered by Solomon Yakobson on Oct 7 2023
Jump to Answer
Comments
Post Details
Added on Oct 7 2023
4 comments
368 views