SQL Server 2000中按非字母顺序排序的字段顺序

3
我正在尝试按照一组不按字母顺序排列的名称来订购物品。在完成该列表后,我将尝试继续以字母顺序排列其余项目,而不包括最初选择的项目。
例如:
输入:
print 'Results:'  
select * from Geniuses
    order by ('Charles Babbage', 
              'Albert Einstein', 
              'Adrien-Marie Legendre', 
              'Niels Henrik Abel')  

然后最后按字母顺序排列剩下的...
Results:
Charles Babbage ... details
Albert Einstein ...
Adrien-Marie Legendre ...
Niels Henrik Abel ...
Arthur Cayley ...
...

你最初选择的列表是有限的还是动态的? - squillman
它就像一个特定用户选择的一小部分最喜爱的天才列表一样有限。但是另一个用户可能会从同样的天才列表中选择不同的人。 - RetroCoder
好的,那么它是动态的。抱歉,我是指静态而不是有限的... - squillman
2个回答

10
select * from Geniuses
order by
    -- First, order by your set order...
    case FullName
        when 'Charles Babbage' then 1
        when 'Albert Einstein' then 2
        when 'Adrien-Marie Legendre' then 3
        when 'Niels Henrik Abel' then 4
        else 5 
    end,
    -- Then do a secondary sort on FullName for everyone else.
    FullName

编辑:

我看到你的评论说它可以由每个用户进行配置。在这种情况下,你需要有一个FavoriteGeniuses表来跟踪哪个用户喜欢哪个Geniuses,并在该表中指定排序顺序:

select * 
from 
    Geniuses g left join
    FavoriteGeniuses fg 
        ON fg.GeniusID = g.GeniusID 
        AND fg.UserID = @UserID
order by
    -- The higher the number, the first up on the list.
    -- This will put the NULLs (unspecified) below the favorites.
    fg.SortPriority DESC, 
    f.FullName

4

试试这样:

select * from Geniuses
order by
   case when columnName = 'Charles Babbage' then 0
     when columnName = 'Albert Einstein' then 1
     when columnName = 'Adrien-Marie Legendre' then 2
     when columnName = 'Niels Henrik Abel' then 3
     else 4
     end,
   columName

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接