0

我需要一些帮助。我有下表(示例如下所示):

EmpID     | Type | timestamp           | block_id
----------------------------------------------------
    1     |  'R' | 2018-04-15 01:13:15 | AB12D
    1     |  'P' | 2018-04-15 05:13:15
    1     |  'P' | 2018-04-15 05:13:15
    1     |  'P' | 2018-04-15 05:13:15
    1     |  'D' | 2018-04-15 07:13:15
    1     |  'D' | 2018-04-15 08:13:15
    1     |  'D' | 2018-04-15 10:13:15
    1     |  'R' | 2018-04-15 13:13:00 | 1X1#1
    1     |  'P' | 2018-04-15 13:15:15
    1     |  'P' | 2018-04-15 13:15:15
    1     |  'P' | 2018-04-15 13:15:15
    1     |  'D' | 2018-04-15 14:13:00
    1     |  'D' | 2018-04-15 15:13:00
    1     |  'D' | 2018-04-15 16:13:37
    2     |  'R' | 2018-04-15 04:15:00 | __08XA
    2     |  'P' | 2018-04-15 04:20:00
    2     |  'D' | 2018-04-15 05:11:33

我正在尝试获得如下输出:

EmpID  | begin_timestamp     | end_timestamp      | block_id | P_count | D_count
---------------------------------------------------------------------------------
1      | 2018-04-15 01:13:15 |2018-04-15 10:13:15 | AB12D    | 3       | 3
1      | 2018-04-15 13:13:00 | 2018-04-15 16:13:37| 1X1#1    | 3       | 3
2      | 2018-04-15 04:15:00 | 2018-04-15 05:11:33| __08XA   | 1       | 1

即,这些是某种块,每个 empId 可以有多个块。所以从上面的示例表中,empID '1' 有 2 个块(从第 1 行开始到第 7 行),第 2 个块从第 8 行到第 14 行;empID 2 有 1 个块,第 14 行到第 16 行。

block_id是一个字母数字字段,可以有任何随机值。此外,该表未按上述顺序排列,仅用于说明目的。我在红移,到目前为止有以下查询:

select CAST(timestamp AS DATE) as date, execution_id, min(timestamp) as begin_timestamp, max(timestamp) as end_timestamp, new_block_id, 
sum(case when Type = 'P' then 1 else 0 end) as P_count,
              sum(case when Type = 'D' then 1 else 0 end) as D_count
from (
    select *,
    max(block_id) over (partition by EmpID order by timestamp ROWS BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW) new_block_id  from myTable
) d
group by EmpID, new_block_id, CAST(timestamp AS DATE)
order by EmpID, new_block_id, CAST(timestamp AS DATE)

但这不起作用,因为它没有给出准确的结果。请帮忙!!

4

1 回答 1

0

你真正想要的是lag(ignore nulls),但 Postgres 不支持。

相反,您可以使用非 NULL 值的时间戳并使用它来定义组。

select date_trunc('day', timestamp) as date, execution_id,
       min(timestamp) as begin_timestamp, max(timestamp) as end_timestamp,
       max(block_id) as block_id,
       sum(case when Type = 'P' then 1 else 0 end) as P_count,
       sum(case when Type = 'D' then 1 else 0 end) as D_count
from (select t.*,
             max(case when block_id is not null then timestamp end) over 
                 (partition by EmpId
                  order by timestamp
                  rows between unbounded preceding and current row
                 ) as grp
      from myTable t
     ) t
group by empId, grp, date_trunc('day', timestamp)
于 2018-04-19T02:27:22.227 回答