我需要一些帮助。我有下表(示例如下所示):
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)
但这不起作用,因为它没有给出准确的结果。请帮忙!!