我有1个表列id,serail_no,已创建。
我的显示是这样的
id serail_no created
1 1142B00072 2012-11-05 11:36:00
2 1142B00072 2012-12-20 14:57:54
3 1142B00072 2012-12-28 13:20:54
4 1142B11134 2012-11-25 14:57:54
5 1142B11134 2013-01-16 16:42:34
所以现在我想要这样的输出。
3 1142B00072 2012-12-28 13:20:54
5 1142B11134 2013-01-16 16:42:34
答案 0 :(得分:3)
您可以使用子查询获取每个serial_no
的最新记录。然后,子查询的结果将返回原始表,以便您可以获取其他列。
SELECT a.*
FROM tableName a
INNER JOIN
(
SELECT serial_no, MAX(created) max_date
FROM tableName
GROUP BY serial_no
) b ON a.serial_no = b.serial_no AND
a.created = b.max_date
答案 1 :(得分:-1)
另一个解决方案:使用排名功能
select * from (with t as
(select 1 id , '1142B00072' as serail_no ,to_date('2012-11-05 11:36:00','yyyy-mm-dd hh24:mi:ss') created from dual
union all
select 2 id , '1142B00072' as serail_no ,to_date('2012-12-20 14:57:54' ,'yyyy-mm-dd hh24:mi:ss') created from dual
union all
select 3 id , '1142B00072' as serail_no ,to_date('2012-12-28 13:20:54','yyyy-mm-dd hh24:mi:ss') created from dual
union all
select 4 id , '1142B11134' as serail_no ,to_date('2012-11-25 14:57:54' ,'yyyy-mm-dd hh24:mi:ss') created from dual
union all
select 5 id , '1142B11134' as serail_no ,to_date('2013-01-16 16:42:34','yyyy-mm-dd hh24:mi:ss') created from dual)
select id,serail_no,created,rank() over ( partition by SERAIL_NO order by created desc ) rn from t)
where rn=1`