select *
from t
where (customer, status_time) in (
select customer, max(status_time) from t group by customer
)
在postgresql中,您可以使用 DISTINCT ON . 在最坏的情况下,您需要使用一个相关的子查询;它比较慢,但可以做这项工作。 为sql server编辑 在sql server中,可以使用cte预计算最大状态时间。例如,您可以执行以下操作:
with
a as (
select customer, max(status_time) as max_time
from t
group by customer
)
select
from t
join a on a.customer = t.customer
and a.max_time = t.status_time
1条答案
按热度按时间omjgkv6w1#
您没有指定正在使用的数据库。如果它实现元组相等,则可以执行以下操作:
在postgresql中,您可以使用
DISTINCT ON
. 在最坏的情况下,您需要使用一个相关的子查询;它比较慢,但可以做这项工作。为sql server编辑
在sql server中,可以使用cte预计算最大状态时间。例如,您可以执行以下操作: