查询表中相同字段按时间排序(时间最大)的记录,该如何处理
发布时间:2010-05-20 14:01:29 文章来源:www.iduyao.cn 采编人员:星星草
查询表中相同字段按时间排序(时间最大)的记录
表 executions
id build_id tester_id execution_ts status testplan_id tcversion_id
653 114 1 2011-05-29 19:36:42 p 3609 8469
654 114 1 2011-05-29 19:39:15 f 3609 8469
如果要查询出tcversion_id相同记录中 execution_ts 时间最大的那条记录,请问sql如何写?
以下sql语句查询出来的结果不正确
SELECT id,tcversion_id ,MAX(execution_ts) ,status FROM executions GROUP BY tcversion_id
该方法可以查询出,但速度特别慢,executions有10万条数据,查询时间至少半个小时了,不可行,请高手赐教!
select * from executions t
where not exists (select 1 from executions where tcversion_id=t.tcversion_id and execution_ts>t.execution_ts)
------解决方案--------------------
select * from executions t
where not exists (select 1 from executions where tcversion_id=t.tcversion_id and execution_ts>t.execution_ts)
应该可以 ,在tcversion_id上建立索引试试
也可以
select t.* from executions t inner join
(select tcversion_id,max(execution_ts) as ma from executions group by tcversion_id) b
on t.tcversion_id=b.tcversion_id and t.execution_ts=b.ma
------解决方案--------------------
参考下贴中的多种方法
http://topic.csdn.net/u/20091231/16/2f268740-391e-40f2-a15e-f243b2c925ab.html
[征集]分组取最大N条记录方法征集,及散分....
------解决方案--------------------
建议你在 executions 表上创建 (tcversion_id,execution_ts)的联合索引。
------解决方案--------------------
select t.* from executions t inner join
(select tcversion_id,max(execution_ts) as ma from executions group by tcversion_id) b
on t.tcversion_id=b.tcversion_id and t.execution_ts=b.ma
这个是可以的
10W条数据要半小时 太慢了吧。。。
------解决方案--------------------
什么数据库?
如果 在tcversion_id上建立索引速度不明显,建立
tcversion_id、execution_ts复合索引试试
友情提示:
信息收集于互联网,如果您发现错误或造成侵权,请及时通知本站更正或删除,具体联系方式见页面底部联系我们,谢谢。
其他相似内容:
-
ebs物料分类sql
SELECT mc.description
,mc.*
FROM apps.mtl_categories mc
,apps.mtl_category_sets ...
-
[20170916]sqlplus set array最小2补充.txt
[20170916]sqlplus set array最小2补充.txt
--//以前写的贴子,链接http://blog.itpu...
-
解决SQL Server中between and数据不包含的问题
SQL Server中的between and基本用法大家耳熟能详,这里不说它的用法,就说最近...
-
sql 查询结果作为数据进行添加,where in 子查询
查询结果作为数据进行添加
INSERT INTO a ( Aid, Atitle, Url, Pic1 ) SELECT c ...
-
Sql SqlServer 脏读、 不可复读和幻读的理解
sql SqlServer 不可复读和幻读的定义,是根据自己的理解写出来的,不是真正的定义,哈...
-
SQLServer 2008R2 清理日志文件
设置数据库为简单模式
2.收缩日志文件
3.恢复数据库为完整模式
1楼dgdyq漏了一步,4. 对数...
-
informix的常用SQL语句
1、创建数据库
eg1. 创建不记录日志的库testdb,参考语句如下:
CREATE DATABA...
-
(转)SqlServer 排名函数(row_number、rank、dense_rank)的比较
[sql] view plain copy
print?
排名函数是SQL Se...
-
spark sql 高效迭代计算
如下sql代码离线计算指标,spark 直接执行 sql,每一个create table ... select 语句都是一个action,创...
-
SQL各种连接——自连接、内连接、外连接、交叉连接的使用
首先准备了两个表 (Student 和 Course),其中 Student 表中的 C_S_Id 字段...