使用 UNION 函数的 SELECT 语句的顺序?

Order of SELECT statements using the UNION function?

如何使用 UNION 函数对 select 语句进行排序? 我希望输出是 select 语句的顺序。 EG T1 然后 T2 然后 T3.

查询:

select 'T1' as Table, sum(col1) from table1
group by 'T1'
UNION
select 'T2' as Table, sum(col2) from table2
group by 'T1'
UNION
select 'T3' as Table, sum(col3) from table3
group by 'T1'

order by 'T1', 'T2', 'T3'

预期输出:

Table Another header
T1 45
T2 90
T3 71

您可以添加辅助列进行排序,如下:

使用 CTE

With CTE As (
select 0 as Sort, 'T1' as "Table", sum(col1) as "Sum" from table1
UNION
select 1 as Sort, 'T2' as "Table", sum(col2) as "Sum" from table2
UNION
select 2 as Sort, 'T3' as "Table", sum(col3) as "Sum" from table3
)
Select "Table", "Sum"
From CTE
Order by Sort

它也应该使用子查询,因为子查询扫描将应用于排序的输入。

Select "Table", "Sum"
From (
select 0 as Sort, 'T1' as "Table", sum(col1) as "Sum" from table1
UNION
select 1 as Sort, 'T2' as "Table", sum(col2) as "Sum" from table2
UNION
select 2 as Sort, 'T3' as "Table", sum(col3) as "Sum" from table3
Order by Sort   
) As T