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

How to fetch data from first row to a particular row

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

I have two tables table 1 (log_audit table) table 2 is (final_log table)

table 1 data looks like below

enter image description here

table 2 data looks like below

enter image description here

Here user "D" actual resolver of the customer ticket. so we want the data from ticket start time (i.e 2020-05-01 10:00:00) to till first assigned to the user "D" (here the ticket first assigned to "D" on "2020-05-01 10:20:00")

The expected output should be like from 1st row to till 4th row

enter image description here

please help me on this.

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

You can use a left join and window functions:

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;