Warm tip: This article is reproduced from serverfault.com, please click

sql-如何从第一行到特定行获取数据

(sql - How to fetch data from first row to a particular row)

发布于 2020-11-30 18:32:56

我有两个表,表1(log_audit表),表2是(final_log表)

表1数据如下所示

在此处输入图片说明

表2数据如下所示

在此处输入图片说明

用户“ D”在这里是用户票证的实际解析器。因此我们希望数据从票证开始时间(即2020-05-01 10:00:00)到直到首次分配给用户“ D”(此处票证在“ 2020-05-01 10”上首先分配给“ D”)的数据:20:00“)

预期的输出应该是从第一行到第四行

在此处输入图片说明

请帮助我。

Questioner
kalyan4uonly
Viewed
0
Gordon Linoff 2020-12-01 10:35:01

你可以使用left join和窗口函数:

select la.*
from (select la.*,
             min(case when fl.actual_user is not null then la.start_time end) over (partition by la.ticket_id) as actual_user_start_time
      from log_audit la left join
           final_log fl
           on fl.actual_user = t.user
     ) la
where start_time <= actual_user_start_time;