SQL将ALL添加到select语句

时间:2014-04-02 08:28:13

标签: sql sql-server-2008

我有一个显示公司列表的select语句。

SELECT distinct [Company]
  FROM [Records]

如何添加条目" ALL"作为返回列表中的第一项?

4 个答案:

答案 0 :(得分:2)

使用union allorder 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]