4

我想显示这样的数据:

Column1        Column2
-----------------------
TotalAvg         60%

A1               50%

B1               70%

Z1               60%

我的 sql 脚本类似于:

select 'Total Avg' as Column1,'60%' as Column2

union

select Column1,Column2 from tblAvg

我得到的结果是这样的:

Column1        Column2
------------------------

A1               50%

B1               70%

TotalAvg         60%

Z1               60%

问题:我想删除默认排序并希望得到我们正在执行联合表的顺序。

4

4 回答 4

8

您可以添加一个指定结果顺序的列:

select 'Total Avg' as Column1,'60%' as Column2, 1 as OrderCol
union
select Column1,Column2, 2 from tblAvg
order by OrderCol

如果没有order by,数据库可以自由地以任何顺序返回行。

于 2012-08-24T13:21:00.053 回答
1
SELECT
  *
FROM
(
  select 0 as pos, 'Total Avg' as Column1, '60%' as Column2
  union
  select 1 as pos,                Column1,          Column2 from tblAvg 
)
  AS data
ORDER BY
  pos, column1, column2
于 2012-08-24T13:21:21.763 回答
1

你可以做这样的事情

select * From
(
select 'Total Avg' as Column1,'60%' as Column2, 1 as ItemOrder

union

select Column1,Column2,2 from tblAvg
) innertable
Order By ItemOrder
于 2012-08-24T13:21:29.627 回答
0

尝试使用

select 'Total Avg' as Column1,'60%' as Column2

union all

select Column1,Column2 from tblAvg
于 2012-08-24T13:20:50.767 回答