在SQLServer2005中实现部分排序查询
我必须以这样一种方式显示记录,即某些选定的记录应排在第一位。在此之后,另一条记录以排序方式从同一个表中出现 例如,如果我选择stateID=5的状态,那么相应的记录应该首先出现。在此之后,另一个记录应以排序方式出现 为此,我尝试了union,但它以排序方式显示了所有内容在SQLServer2005中实现部分排序查询,sql,sql-server,sorting,Sql,Sql Server,Sorting,我必须以这样一种方式显示记录,即某些选定的记录应排在第一位。在此之后,另一条记录以排序方式从同一个表中出现 例如,如果我选择stateID=5的状态,那么相应的记录应该首先出现。在此之后,另一个记录应以排序方式出现 为此,我尝试了union,但它以排序方式显示了所有内容 select state from statemaster where stateid=5 union all select state from statemaster where not stateid =5 order b
select state from statemaster where stateid=5
union all
select state from statemaster
where not stateid =5
order by state
谢谢此选项将用于首先为您提供stateid=5的状态,然后是其他状态。第二个排序标准是状态
Select state
From statemaster
Order By
Case When stateid = 5 Then 0 Else 1 End,
state
如果您有两个以上的联合,这将非常有用
select 1 as sort_id, state from statemaster where stateid=5
union all
select 2 as sort_id, state from statemaster
where stateid between 1 and 4
union all
select 3 as sort_id, state from statemaster
where stateid > 5
order by sort_id, state
+1.甚至不知道这是可能的。很好的新把戏放进我的包里。我同意你的看法。这是一个很好的答案。