如何根据 Oracle 中两个查询的并集结果进行排序?

How can you order by the result of union of two queries in Oracle?

我有以下 Oracle SQL,它工作正常,但第二个查询的结果在第一行而不是第二行返回。

如何维护输出中的顺序,以便第一个查询结果显示在第一行,第二个显示在第二行,依此类推。

甲骨文SQL

select
  sum(a.transaction_amount) as transaction_amount,
  'Last 30 Days Debit Volume (Current Year)' as sales_volume
from
  payment_transaction a,
  payment_settlement b
where
  a.transaction_status = 'S'
  and b.settlement_type = 'D'
  and trunc(b.transaction_date) > sysdate - 30
  and a.payment_transaction_id = b.payment_transaction_id

union

select
  sum(a.transaction_amount) as transaction_amount,
  'Last 30 Days Credit Volume (Current Year)' as sales_volume
from
  payment_transaction a,
  payment_settlement b
where
  a.transaction_status = 'S'
  and b.settlement_type = 'C'
  and trunc(b.transaction_date) > sysdate - 30
  and a.payment_transaction_id = b.payment_transaction_id

当前输出

TRANSACTION_AMOUNT       SALES_VOLUME
6272                     Last 30 Days Credit Volume (Current Year)
10719                    Last 30 Days Debit Volume (Current Year)

预期输出

TRANSACTION_AMOUNT       SALES_VOLUME
10719                    Last 30 Days Debit Volume (Current Year)
6272                     Last 30 Days Credit Volume (Current Year)

只需添加一个虚拟列进行排序

 SELECT <yourfields>
 FROM (
      SELECT 1 as dummy, <yourfields>
      FROM Query1
      UNION
      SELECT 2 as dummy, <yourfields>
      FROM Query2
     ) T
ORDER BY dummy