日期:2014-05-17  浏览次数:20730 次

oracle一个SQL语句的,
先分组 在查询每组最新三条数据

比如,
1 portal.war 1 2012-08-22 15:58:22 +0800
2 portal.war 1 2012-08-21 15:58:22 +0800
3 portal.war 0 2012-08-20 15:58:22 +0800
4 portal.war 1 2012-08-19 15:58:22 +0800
5 portal2.war 1 2012-08-20 15:58:22 +0800
6 portal3.war 1 2012-08-19 15:58:22 +0800
7 portal.war 0 2012-08-17 15:58:22 +0800



显示结果为
1 portal.war 1 2012-08-22 15:58:22 +0800
2 portal.war 1 2012-08-21 15:58:22 +0800
3 portal.war 0 2012-08-20 15:58:22 +0800
5 portal2.war 1 2012-08-20 15:58:22 +0800
6 portal3.war 1 2012-08-19 15:58:22 +0800


谁帮忙看下吧,实在没写出来的

------解决方案--------------------
SQL code


--假设对应列为id,name,val,dt,还过这种速度不快
select id,name,val,dt from
(select row_number() over(partition by name order by dt desc) rn,id,name,val,dt from 你的表) t
where rn<4;