Yahoo Answers is shutting down on 4 May 2021 (Eastern Time) and the Yahoo Answers website is now in read-only mode. There will be no changes to other Yahoo properties or services, or your Yahoo account. You can find more information about the Yahoo Answers shutdown and how to download your data on this help page.

Oracle SQL, How can you do an order by abc without having abc in select?

Select XYZ

FROM table

UNION ALL

Select Sum(XYZ)

FROM table;

Placing an ORDER BY abc at the end is just giving me errors. I want to have the order done in the numbers in abc but not show the column in the report.

2 Answers

Relevance
  • 9 years ago

    If what you're trying to do is ensure the summation occurs last, try this:

    SELECT XYZ, label AS "" FROM

    (SELECT 1 AS OrderIt, XYZ, '"" AS label FROM table

    UNION ALL

    SELECT 2, SUM(XYZ), "(Total)" FROM table)

    ORDER BY OrderIt

    This sort of thing is about the only situation I've found where ordering by a non-selected column is useful.

  • 9 years ago

    select xyz from (

    Select XYZ, abc FROM table

    UNION ALL

    Select Sum(XYZ) xyz, sum(abc) abc FROM table

    )

    order by abc;

Still have questions? Get answers by asking now.