Sql 查询每个计划中的最新序列
我有一个表,其中可能包含每个计划的多个序列。我想从每个序列中输出一条(最新的)记录。下面是该表的一个示例,以及我要查找的输出: 表格样本: 预期产出:Sql 查询每个计划中的最新序列,sql,Sql,我有一个表,其中可能包含每个计划的多个序列。我想从每个序列中输出一条(最新的)记录。下面是该表的一个示例,以及我要查找的输出: 表格样本: 预期产出: 表名是dataschedule-您能告诉我如何最好地编写sql查询以获取此数据吗?谢谢大家! 嗯。如果需要最新序列,可以使用相关子查询: select t.* from t where t.sequence = (select max(t2.sequence) from t t2
表名是dataschedule-您能告诉我如何最好地编写sql查询以获取此数据吗?谢谢大家! 嗯。如果需要最新序列,可以使用相关子查询:
select t.*
from t
where t.sequence = (select max(t2.sequence)
from t t2
where t2.schedule = t.schedule and t2.id = t.id
);
在大多数数据库中,它在
(id、schedule、sequence)
上有一个索引,性能非常好。请尝试一下,我还没有测试过它,因为您无法提供insert和create表脚本。只需用表名替换表名即可
select table_name.*
from table_name, (select ID,schedule,max(sequence) from table_name group by ID,schedule) a
where
table_name.ID = a.ID and
table_name.schedule = a.schedule ;