3

我有 1 个表列 id,serail_no,created。

我的显示是这样的

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
4

2 回答 2

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
于 2013-01-30T05:29:19.163 回答
-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`
于 2013-01-30T05:44:13.077 回答