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 回答