select a.* from tname a order by (select count(*) from tname where Type=a.Type and Id<=a.Id) desc,Id
select a.* from tname a order by (select count(*) from tname where Type=a.Type and Id>=a.Id) ,Id 可以不用倒序的,查找大於等於的即可。
declare @a table ( id int, type char(1) ) insert into @a select 1,'A' union all select 2,'B' union all select 3,'C' union all select 4,'A' union all select 5,'B' union all select 6,'C' union all select 7,'A' union all select 8,'B' union all select 9,'C'select * from @a a order by (select count(1) from @a where id<a.id and type=a.type)desc,id
a.*
from
tname a
order by
(select count(*) from tname where Type=a.Type and Id<=a.Id) desc,Id
select
a.*
from
tname a
order by
(select count(*) from tname where Type=a.Type and Id>=a.Id) ,Id
可以不用倒序的,查找大於等於的即可。
(
id int,
type char(1)
)
insert into @a
select 1,'A' union all
select 2,'B' union all
select 3,'C' union all
select 4,'A' union all
select 5,'B' union all
select 6,'C' union all
select 7,'A' union all
select 8,'B' union all
select 9,'C'select *
from @a a
order by (select count(1) from @a where id<a.id and type=a.type)desc,id