sql 的日期问题 - 仅显示一行
Date problem with sql - displaying only one row
我已经 table employee_id、school_end(日期)、school_name
一些员工有几个学校名称和日期。
我只想在查询中看到最近的学校和日期 employee_id
提前致谢
I want to see in query only the latest school and date for one employee_id
这个 top-1-per-group 问题的一个跨数据库解决方案是使用相关子查询进行过滤:
select t.*
from mytable t
where t.school_end = (
select max(t1.school_end) from mytable t1 where t1.employee_id = t.employee_id
)
为了性能,您需要 (employee_id, school_end)
上的索引。
根据您的数据库,可能会提供更高效的选项(例如,Postgres 具有 distinct on
语法)。
我已经 table employee_id、school_end(日期)、school_name 一些员工有几个学校名称和日期。
我只想在查询中看到最近的学校和日期 employee_id
提前致谢
I want to see in query only the latest school and date for one employee_id
这个 top-1-per-group 问题的一个跨数据库解决方案是使用相关子查询进行过滤:
select t.*
from mytable t
where t.school_end = (
select max(t1.school_end) from mytable t1 where t1.employee_id = t.employee_id
)
为了性能,您需要 (employee_id, school_end)
上的索引。
根据您的数据库,可能会提供更高效的选项(例如,Postgres 具有 distinct on
语法)。