sql排序分组问题

fangquan1980 2010-09-27 03:41:27
有以下数据,其中2列:
a b c....
----------------------
1 b1
2 b2
3 b1
4 b3
5 b4
6 b2


要求效果
a b c....
---------------------
1 b1
3 b1
2 b2
6 b2
4 b3
5 b4

即按照b出现的先后顺序排序然后分组
...全文
48 5 打赏 收藏 转发到动态 举报
写回复
用AI写文章
5 条回复
切换为时间正序
请发表友善的回复…
发表回复
dawugui 2010-09-27
  • 打赏
  • 举报
回复
create table tb(a int, b varchar(10),c int)
insert into tb values(1 ,'b1',null)
insert into tb values(2 ,'b2',null)
insert into tb values(3 ,'b1',null)
insert into tb values(4 ,'b3',null)
insert into tb values(5 ,'b4',null)
insert into tb values(6 ,'b2',null)
go
select m.* from tb m,
(select * from tb t where a = (select min(a) from tb where b = t.b)) n
where m.b = n.b
order by n.a

drop table tb

/*
a b c
----------- ---------- -----------
1 b1 NULL
3 b1 NULL
2 b2 NULL
6 b2 NULL
4 b3 NULL
5 b4 NULL

(所影响的行数为 6 行)
*/
dawugui 2010-09-27
  • 打赏
  • 举报
回复
select m.* from tb m,
(select * from tb t where a = (select min(a) from tb where b = t.b)) n
where m.b = n.b
order by n.a
喜-喜 2010-09-27
  • 打赏
  • 举报
回复
select * from tb order by b,a
dawugui 2010-09-27
  • 打赏
  • 举报
回复
select * from tb order by b , a
百年树人 2010-09-27
  • 打赏
  • 举报
回复
select a.*
from tb a,
(select b,min(a) as px from tb group by b)b
where a.b=b.b
order by px

22,207

社区成员

发帖
与我相关
我的任务
社区描述
MS-SQL Server 疑难问题
社区管理员
  • 疑难问题社区
  • 尘觉
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧