postgresql重新组合连续一小时

问题描述 投票:0回答:1

我们的想法是将免费时间分组,以便为​​未来的客户提供预约

我有3张桌子

hour_dimension,包括工作时间

select hour from hour_dimension

|hour   |
|-------|
|09:00  |
|09:30  |
|10:00  |
|10:30  |
|11:00  |
|11:30  |
|12:00  |
|14:00  |
|14:30  |
|15:00  |
|15:30  |
|16:00  |
|16:30  |
|17:00  |
|17:30  |
|18:00  |
|18:30  |
|19:00  |
|19:30  |
|20:00  |

在客户和约会之间建立关系的事件

|id |commentaire|client_id  |sms|state_id   |
---------------------------------------------
|463|           |308        |1  |1          |
|464|           |308        |1  |1          |
|465|           |308        |1  |1          |

包含客户约会的rdv(仅适用于info event.id = rdv.id)

|id |date       |heure_debut|heure_fin  |
-----------------------------------------
|463|2018-02-15 |09:30      |11:00      |
|464|2018-02-15 |14:00      |16:00      |
|465|2018-02-15 |17:00      |18:00      |   

现在,我执行此查询以查找空闲时间:

select h.hour, heure_debut as begin_hour, heure_fin as end_hour
FROM hour_dimension h

left join 
( select * from rdv r 
left join event e ON (r.id = e.id)
where r.date = '2018-02-15' and e.state_id IN (1,2) )_A
ON (h.hour> heure_debut and h.hour < heure_fin)

结果:pic.PNG我包围了免费时间。

现在,我想将免费时间分组并得到以下结果:

|begin| end |duration|
|-----+-----+--------|
|09:00|09:30| 30mins |
|11:00|12:00| 60mins | 12:00 - 14:00 break time
|16:00|17:00| 60mins |
|18:00|20:00| 120mins|
postgresql hour
1个回答
0
投票

我能够在飞行中找到的最佳“中间”解决方案是this小提琴。

我的猜测是你需要一个函数来最终产生你想要的结果。

如果您采用这个小提琴中间结果,将其包含/添加到函数中,从而消除与前一行结束相同的所有行,将它们保存为temp_begin和temp_end并调用

EXTRACT(epoch FROM 
    to_timestamp(temp_end,  'HH24:MI') 
  - to_timestamp(temp_begin,'HH24:MI')
)/60 

在那些临时变量上你应该能够得到你想要的东西

© www.soinside.com 2019 - 2024. All rights reserved.