怎么实现如下数据刷选

kisshexuxia 2010-09-03 12:43:16
A id,xm

B id,date1,date2

A 和B的关系是1对多 (1:N)

select a.xm,b.date1,b.date2,a.id from A a,B b where a.id=b.id


结果

id,xm, date1, date2
1, a ,2010-06-02 2010-09-02
1, a ,2010-07-02 2010-09-02
1, a ,2010-08-02 2010-09-02
2, b ,2010-06-02 2010-09-02
3, c ,2010-07-02 2010-09-02
3, c ,2010-08-02 2010-09-02
.
.
.

现在要得出每个ID的date1最大的条数据如下,SQL要怎么写

id,xm, date1, date2

1, a ,2010-08-02 2010-09-02
2, b ,2010-06-02 2010-09-02

3, c ,2010-08-02 2010-09-02
...全文
73 2 打赏 收藏 转发到动态 举报
写回复
用AI写文章
2 条回复
切换为时间正序
请发表友善的回复…
发表回复
百年树人 2010-09-03
  • 打赏
  • 举报
回复
select a.*,b.date1,b.date2
from a,b
where a.id=b.id
and not exists(select 1 from b t where t.id=b.id and t.date1>b.date1)
hao1hao2hao3 2010-09-03
  • 打赏
  • 举报
回复


select a.id,a.xm,max(b.date1),b.date2
from A a,B b where a.id=b.id
group by a.id,a.xm,b.date2

22,209

社区成员

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

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