我有一个显示公司列表的select语句。
SELECT distinct [Company]
FROM [Records]
如何添加条目" ALL"作为返回列表中的第一项?
答案 0 :(得分:2)
使用union all
和order by
:
select company
from ((select distinct company, 1 as ordering from records)
union all
(select 'ALL', 0)
) t
order by ordering;
在实践中,以下似乎有效:
select 'ALL' as company
union all
select distinct company from records;
但是,SQL Server不保证按顺序执行这些操作。在实践中,我从未发现过这样的陈述不会将ALL
放在第一位的情况,但据我所知,这并不能保证。
答案 1 :(得分:1)
您可以使用UNION ALL
并添加订单:
SELECT [Company] FROM
(SELECT 'All' as [Company], 0 as RecordOrder
UNION ALL
SELECT distinct [Company], 1 as RecordOrder
FROM [Records]) X
ORDER BY RecordOrder
答案 2 :(得分:0)
尝试使用union all
SELECT 'ALL'
UNION ALL
SELECT distinct [Company]
FROM [Records]
答案 3 :(得分:0)
SELECT 'ALL'
UNION ALL
SELECT distinct [Company]
FROM [Records]