从给定数据列表中查询最新记录
Query latest record from a list of given data
我有一个需求是查询最新记录的数据列表。这是我的示例 table(省略主键)
col1| createtime
d1 | 2013-01-31 22:04:15
d1 | 2014-01-31 22:04:15
d1 | 2015-01-31 22:04:15
d2 | 2013-01-31 22:04:15
d2 | 2014-01-31 22:04:15
d2 | 2014-02-31 22:04:15
d2 | 2015-01-31 22:04:15
d3 | 2013-01-31 22:04:15
d3 | 2014-01-31 22:04:15
d3 | 2014-01-31 22:04:15
d3 | 2015-01-31 22:04:15
d4 | 2013-01-31 22:04:15
d4 | 2014-01-31 22:04:15
d4 | 2015-01-31 22:04:15
给出col1的数据列表。例如,给出的数据列表是[d3, d4]。我的查询结果应该是 rows
[(d3 2015-01-31 22:04:15), (d4 2015-01-31 22:04:15)]
因为d3的最新记录是2015-01-31 22:04:15
,d4的最新记录是2015-01-31 22:04:15
这是否可以不使用 sql 程序?
如果只有两列,只需使用 group by
:
select t.col1, max(t.createtime)
from table t
where t.col1 in ('d3', 'd4')
group by t.col1;
如果您有两列以上,我认为以下方法可行:
select t.*
from table t
where t.col1 in ('d3', 'd4') and
not exists (select 1
from table t2
where t2.col1 = t.col1 and
t2.createtime > t.createtime
);
您也可以使用 table 表达式
;WITH C AS(
SELECT RANK() OVER (PARTITION BY col1 ORDER BY createtime DESC) AS Rnk
,col1
,createtime
FROM tableName
)
SELECT col1, createtime FROM C WHERE Rnk = 1
以下示例可以帮助您解决问题:
select id, max(time_stamp)
from (select 'd1' as id, '2013-01-31 22:04:15' as time_stamp from dual
union all
select 'd1', '2014-01-31 22:04:15' from dual
union all
select 'd1', '2015-01-31 22:04:15' from dual
union all
select 'd2', '2013-01-31 22:04:15' from dual
union all
select 'd2', '2014-01-31 22:04:15' from dual
union all
select 'd2', '2014-02-31 22:04:15' from dual
union all
select 'd2', '2015-01-31 22:04:15' from dual
union all
select 'd3', '2013-01-31 22:04:15' from dual
union all
select 'd3', '2014-01-31 22:04:15' from dual
union all
select 'd3', '2014-01-31 22:04:15' from dual
union all
select 'd3', '2015-01-31 22:04:15' from dual
union all
select 'd4', '2013-01-31 22:04:15' from dual
union all
select 'd4', '2014-01-31 22:04:15' from dual
union all
select 'd4', '2015-01-31 22:04:15' from dual)
where id in ('d3', 'd4')
group by id;
如果有更多列,也将这些列添加到按字段分组。
我有一个需求是查询最新记录的数据列表。这是我的示例 table(省略主键)
col1| createtime
d1 | 2013-01-31 22:04:15
d1 | 2014-01-31 22:04:15
d1 | 2015-01-31 22:04:15
d2 | 2013-01-31 22:04:15
d2 | 2014-01-31 22:04:15
d2 | 2014-02-31 22:04:15
d2 | 2015-01-31 22:04:15
d3 | 2013-01-31 22:04:15
d3 | 2014-01-31 22:04:15
d3 | 2014-01-31 22:04:15
d3 | 2015-01-31 22:04:15
d4 | 2013-01-31 22:04:15
d4 | 2014-01-31 22:04:15
d4 | 2015-01-31 22:04:15
给出col1的数据列表。例如,给出的数据列表是[d3, d4]。我的查询结果应该是 rows
[(d3 2015-01-31 22:04:15), (d4 2015-01-31 22:04:15)]
因为d3的最新记录是2015-01-31 22:04:15
,d4的最新记录是2015-01-31 22:04:15
这是否可以不使用 sql 程序?
如果只有两列,只需使用 group by
:
select t.col1, max(t.createtime)
from table t
where t.col1 in ('d3', 'd4')
group by t.col1;
如果您有两列以上,我认为以下方法可行:
select t.*
from table t
where t.col1 in ('d3', 'd4') and
not exists (select 1
from table t2
where t2.col1 = t.col1 and
t2.createtime > t.createtime
);
您也可以使用 table 表达式
;WITH C AS(
SELECT RANK() OVER (PARTITION BY col1 ORDER BY createtime DESC) AS Rnk
,col1
,createtime
FROM tableName
)
SELECT col1, createtime FROM C WHERE Rnk = 1
以下示例可以帮助您解决问题:
select id, max(time_stamp)
from (select 'd1' as id, '2013-01-31 22:04:15' as time_stamp from dual
union all
select 'd1', '2014-01-31 22:04:15' from dual
union all
select 'd1', '2015-01-31 22:04:15' from dual
union all
select 'd2', '2013-01-31 22:04:15' from dual
union all
select 'd2', '2014-01-31 22:04:15' from dual
union all
select 'd2', '2014-02-31 22:04:15' from dual
union all
select 'd2', '2015-01-31 22:04:15' from dual
union all
select 'd3', '2013-01-31 22:04:15' from dual
union all
select 'd3', '2014-01-31 22:04:15' from dual
union all
select 'd3', '2014-01-31 22:04:15' from dual
union all
select 'd3', '2015-01-31 22:04:15' from dual
union all
select 'd4', '2013-01-31 22:04:15' from dual
union all
select 'd4', '2014-01-31 22:04:15' from dual
union all
select 'd4', '2015-01-31 22:04:15' from dual)
where id in ('d3', 'd4')
group by id;
如果有更多列,也将这些列添加到按字段分组。