A B
1 20010107
2 20100101
3 20010208
1 20010108
3 20010105
我想显示成如下效果
A B
3 20010105
3 20000208
1 20010107
1 20010108
2 20100101
先按A分类,再按各分类的第一条排序,该如何实现
1 20010107
2 20100101
3 20010208
1 20010108
3 20010105
我想显示成如下效果
A B
3 20010105
3 20000208
1 20010107
1 20010108
2 20100101
先按A分类,再按各分类的第一条排序,该如何实现
declare @table table (A int,B varchar(10))
insert into @table
select 1,'20010107' union all
select 2,'20100101' union all
select 3,'20010208' union all
select 1,'20010108' union all
select 3,'20010105'select a.* from @table a left join(select * from @table b where B=
(select min(B) from @table where A=b.A)) c on a.A=c.A order by c.B,a.B
/*
A B
----------- ----------
3 20010105
3 20010208
1 20010107
1 20010108
2 20100101
*/
declare @table table (A int,B varchar(10))
insert into @table
select 1,'20010107' union all
select 2,'20100101' union all
select 3,'20010208' union all
select 1,'20010108' union all
select 3,'20010105'select *
from @table
order by charindex(','+ltrim(a)+',',',3,1,2,'),B
/**
A B
----------- ----------
3 20010105
3 20010208
1 20010107
1 20010108
2 20100101(5 行受影响)
**/