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

【oracle】排序问题
table 
--------------------------
id,num1,num2, time
1   2    2     2012-11-28 12:32:07 
2   3    3     2012-11-28 12:32:09 
3   2    3     2012-11-28 12:32:08
4   4    8     2012-11-28 12:32:10 
3   5    4     2012-11-28 12:32:17 
2   2    2     2012-11-28 12:32:12
。。。。

取10秒内 按id分组的数据 重复的id取最新的那个
比如编号为2的id有两个分组后 取 2   2    2     2012-11-28 12:32:12最新的那个

查询结果如下
 --------------------------
id,num1,num2, time
1   2    2     2012-11-28 12:32:07 
4   4    8     2012-11-28 12:32:10 
3   5    4     2012-11-28 12:32:17 
2   2    2     2012-11-28 12:32:12

------最佳解决方案--------------------

SELECT a.id,a.num1,a.num2, a.time
FROM tab a,
      (SELECT MAX(time) btime,ID
               FROM tab
               GROUP BY ID ) b

WHERE a.ID=b.ID
AND   a.time=b.btime

试试
------其他解决方案--------------------
select id, num1, num2, time from(
select t.*, row_number() over (partition by id order by time desc) rn from t
) where rn = 1;

------其他解决方案--------------------
来晚了,顶楼上
------其他解决方案--------------------
select distinct id,num1,num2,time from table1 group by id order by time desc 

没试,不知道可不可以
------其他解决方案--------------------
按id分组 我知道 但取最新那个 就不知道怎么写了
------其他解决方案--------------------
引用:
SQL code?



123

select id, num1, num2, time from( select t.*, row_number() over (partition by id order by time desc) rn from t ) where rn = 1;


取的是 不是最新的 分组可以
------其他解决方案--------------------
引用:
SQL code?



123456789

SELECT a.id,a.num1,a.num2, a.timeFROM tab a,       (SELECT MAX(time) btime,ID                FROM tab                GROUP BY ID ) b   WHERE a.ID=b.ID AND   a.time=b.btim……