【问题标题】:Get a list of requests获取请求列表
【发布时间】:2022-11-26 19:36:15
【问题描述】:

有两个表,我需要得到一个 client_id 的列表,它的最后一个请求(标题)还没有被处理。我们认为,如果请求创建后(created_datetime)有调用,那么它已经被处理了。

tasks
client_id created_datetime title
calls
manager_id client_id call_datetime

我认为需要随着时间的推移做一些事情,但我不明白是什么。

select client_id, max(calls.call_datetime)  - max(tasks.created_datetime) as time
from tasks join calls on tasks.client_id = calls.client_id 
group by client_id, call_datetime  

【问题讨论】:

标签: sql postgresql


【解决方案1】:
with maxes as (select client_id, max(calls.call_datetime) latest_call
from calls 
group by client_id)
select tasks.client_id, title 
from tasks 
inner join maxes on maxes.client_id = tasks.client_id
where created_datetime > maxes.latest_call

如果我没理解错,应该是这样

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2017-08-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-04-18
    • 1970-01-01
    • 2020-07-20
    相关资源
    最近更新 更多