如何根据条件在postgres中按列分组获取最后一行

时间:2019-01-02 10:08:14

标签: sql postgresql

我有一张桌子

task_id     timestamp                           event_type
1           "2018-12-20 14:43:53.661153+01"     "open"
1           "2018-12-20 14:43:53.661153+01"     "interrupted"
1           "2018-12-20 14:43:53.661153+01"     "opened"
1           "2018-12-20 14:44:53.661153+01"     "closed"
2           "2018-12-20 14:43:53.661153+01"     "opened"
2           "2018-12-20 14:43:53.661153+01"     "interrupted"
2           "2018-12-20 14:43:53.661153+01"     "opened"
3           "2018-12-20 14:43:53.661153+01"     "opened"
3           "2018-12-20 14:45:53.661153+01"     "closed"

我只需要获取关闭了event_type的任务(这是task_id的最后一行)的数据。 在上面的示例中,由于任务2未关闭,我应该获取task_id 1和3的数据,而不是2的数据。 因此,对于每个任务,如果关闭了最后一行,则应该获得该任务的最后一行与倒数第二行之间的时间差。

task_id     time_diff_minutes
1           1
3           2

3 个答案:

答案 0 :(得分:1)

我们可以通过def Hello(): a = 5 b = 2 print("hello world " + str(a + b)) 进行汇总,然后找出应该同时存在的打开和关闭时间戳记。如果任务实际上有关闭时间戳,则我们将报告它。

task_id

答案 1 :(得分:1)

您可以利用postgres LAG功能。检查以下SQL:-

select test_id, extract(epoch from(event_time - prev_time))/60 as diff
from (select test_id,
LAG(event_time) over(partition by test_id) prev_time
,event_time
,event_type
from test
) a11
where event_type = 'closed'

SQL Fiddle

答案 2 :(得分:0)

select task_id
   ,extract(epoch from(event_time - prev_time))/60 as diff
from 
 ( select task_id,
      row_number()     -- to filter the latest row
      over(partition by task_id
           order by event_time desc) as rn
     ,lead(event_time) -- second last row for that task
      over(partition by task_id
           order by event_time desc) as prev_time
     ,event_time
     ,event_type
   from test
 ) as dt
where rn = 1                -- last event
  and event_type = 'closed' -- must be closed