2012-02-03 122 views
3

我试图按不按字母顺序排列的名称列表来排序项目。完成清单后,我试图按照字母顺序继续其余部分,而不是最初选择的部分。在SQL Server 2000中按字段以字母顺序排序

见例如:

输入:

print 'Results:' 
select * from Geniuses 
    order by ('Charles Babbage', 
       'Albert Einstein', 
       'Adrien-Marie Legendre', 
       'Niels Henrik Abel') 

然后最终排序按字母顺序休息...

OUTPUT:

Results: 
Charles Babbage ... details 
Albert Einstein ... 
Adrien-Marie Legendre ... 
Niels Henrik Abel ... 
Arthur Cayley ... 
... 
+0

您最初选择的列表是有限的还是动态的? – squillman 2012-02-03 17:22:31

+0

其有限性就像一个由特定用户选择的最爱天才的小列表。但另一个用户可能会从同一个天才列表中选择不同的人。 – RetroCoder 2012-02-03 17:24:30

+0

好的,那么它是动态的。对不起,我的意思是静态而不是有限的... – squillman 2012-02-03 17:29:57

回答

8
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