group up 碰到的问题?

haoguozhong 2005-02-27 12:18:42

如何把按照字段A group by,同时得到第一条记录所对应字段B,字段C,字段D

如:
id A B C
1 cc 125 255
2 cc 152 254
3 ff 125 555
4 ff 123 ddd
5 cc 2344 111

得到:
id A B C
1 cc 125 255
3 ff 125 555
...全文
83 6 打赏 收藏 转发到动态 举报
写回复
用AI写文章
6 条回复
切换为时间正序
请发表友善的回复…
发表回复
xluzhong 2005-02-27
  • 打赏
  • 举报
回复
select *
from tablename a
where not exists(select 1 from tablename where a.id>id and a.a=a)
xluzhong 2005-02-27
  • 打赏
  • 举报
回复
select *
from tablename
where id in (select min(id) from tablename group by a)
Softlee81307 2005-02-27
  • 打赏
  • 举报
回复
select *
from tablename k
where id in (select min(id) from tablename where a=k.a group by a)
dazhu2 2005-02-27
  • 打赏
  • 举报
回复
----建表

create table table1(ID1 int,A varchar(8),B varchar(8),C varchar(8))
Insert into table1
select 1,'cc','125','255'
union all select 2,'cc','152','254'
union all select 3,'ff','125','555'
union all select 4,'ff','123','ddd'
union all select 5,'cc','2344','111'




----查询
select * from table1 where ID1 IN (Select min(ID1) FROM table1 group by [A])



-----删除
drop table t1



-
Qihua_wu 2005-02-27
  • 打赏
  • 举报
回复
select * from tablename where id in(select min(id) from tablename group by a)

select a.* from tablename a join (select min(id) id from tablename group by a)b
on a.id=b.id

select *
from tablename a
where not exists(select 1 from tablename where a.id>id and a.a=a)
xiaoxiangqing 2005-02-27
  • 打赏
  • 举报
回复
--方法一:
select * from tablename where id in(select min(id) from tablename group by a)
--方法二:
select a.* from tablename a join (select min(id) id from tablename group by a)b
on a.id=b.id

34,587

社区成员

发帖
与我相关
我的任务
社区描述
MS-SQL Server相关内容讨论专区
社区管理员
  • 基础类社区
  • 二月十六
  • 卖水果的net
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

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