请教一个sql语句的写法

来源:百度知道 编辑:UC知道 时间:2024/05/31 10:49:58
请教一个sql语句的写法。
数据库里的数据如下
ID date no name
1 20090828 1 aaa
2 20090827 3 bbb
1 20090828 2 ccc
2 20090827 1 ddd
3 20090827 1 eee
1 20090823 1 fff

要求写一个sql语句,结果如下
1 20090828 2 ccc
2 20090827 3 bbb
3 20090827 1 eee

就是取得每个ID最大日期,并且最大no的那条数据。
实在写不来了。谢谢各位大侠~~

select 表.* from 表,(select 表.id,date,max(no) mno from 表,(select id,max(date) mdate from 表 group by id) d where 表.id=d.id and 表.date=d.mdate group by 表.id,date) n where 表.id=n.id and 表.date=n.date and 表.no=n.mno

select table.* from table ,(select max(id) max_id ,no from table group by no ) tmp
where table.id = max_id

select top 1 *
from table
group by id
order by date desc,no desc;

试试看,没测试