如何查询不在group by后出现的字段

lyp524 2009-07-31 12:36:01
标题这么写不晓得对不对,具体看例子:

表内容:
id content idplace
1 2009-06-05 上海
1 2009-07-05 广州
2 2009-07-08 北京
3 2009-08-10 北京

要得到的结果
id content idplace
1 2009-06-05 上海
2 2009-07-08 北京
3 2009-08-10 北京

每个id只取一条记录,如遇到相同id的记录,筛选依据是选content字段值小的

原来想用group by ,发现idplace字段没法处理
...全文
428 5 打赏 收藏 转发到动态 举报
写回复
用AI写文章
5 条回复
切换为时间正序
请发表友善的回复…
发表回复
feixianxxx 2009-07-31
  • 打赏
  • 举报
回复
select
*
from
tb t
where
content in (select top 1 content from tb where id=t.id order by content )
水族杰纶 2009-07-31
  • 打赏
  • 举报
回复
select * from tb t where content=(select min(content) from tb where id=t.id )
百年树人 2009-07-31
  • 打赏
  • 举报
回复
或者

select 
*
from
tb t
where
content=(select top 1 content from tb where id=t.id order by content)

水族杰纶 2009-07-31
  • 打赏
  • 举报
回复
select * from tb t where not exists(select 1 from tb where id=t.id and content<t.content) 
百年树人 2009-07-31
  • 打赏
  • 举报
回复
select
*
from
tb t
where
not exists(select 1 from tb where id=t.id and content<t.content)

34,576

社区成员

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

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