0

これは、「デフォルト」を一番上にフロートさせ、残りをソートするために私が思いついたものです。

SELECT        'default' AS name, 'AAA' AS tag
UNION
SELECT        name, name AS tag
FROM            (SELECT DISTINCT TOP (100) PERCENT name
                          FROM            AccountManager
                          WHERE        (name <> 'default')
                          ORDER BY name) AS derivedtbl_1
ORDER BY tag

より複雑でない/より良い方法はありますか?

4

2 に答える 2

3
SELECT name 
FROM AccountManager
GROUP BY name /*To retain DISTINCT semantics from original query*/
ORDER BY CASE WHEN name = 'default' THEN 0 ELSE 1 END, name

注意:

SELECT DISTINCT name 
FROM AccountManager
ORDER BY CASE WHEN name = 'default' THEN 0 ELSE 1 END, name

ORDER BY items must appear in the select list if SELECT DISTINCT is specified.したがって、を使用すると失敗しますGROUP BY

于 2012-06-26T22:53:29.047 に答える
1

並べ替え方法を変更できます。

select name
from AccountManager
order by case name when 'default' then 0 else 1 end, name
于 2012-06-26T22:53:33.163 に答える