是否可以根据开始/结束日期计算有效的换档时间,而停顿了Oracle SQL
我在Oracle
Shift_ID | Timestamp_(Oracle) | type_ |
---|---|---|
00000001 | 17/05/2022 08:00 | 0001 |
00000001 | 17/05/2022 09:00 | 0002 |
00000001 | 17/05/2022 09:15 | 0003 |
0000000001 | 17/05/2022 12: 00 | 0002 |
00000001 | 17/05/2022 13:00 | 0003 |
00000001 | 17/05/2022 15:00 | 15:00 |
type 1是开始时间,类型2是暂停,pause,type 3在暂停之后继续,type 4 is end_time
我想计算有效的偏移时间,我认为这样做的一种方法是计算所有启动和结束时间的总和,例如:(
sum(类型0002) + sum(类型0004))) - (sum(类型0001) + sum (类型0003))
但是如何在Oracle SQL中执行此操作?
我尝试了: 选择shift_id,sum(to_number_to_char(timestamp _,'sssss'),其中(type_ = 0002或类型为0004)组通过shift_id,
然后我得到了一个结果 00000001,82442 但是总和不是合适的总和,它仅在午夜之后以秒数计算。
问题:如何获得类似的结果: 00000001,05:45
I have an table in Oracle
shift_id | timestamp_(oracle) | type_ |
---|---|---|
00000001 | 17/05/2022 08:00 | 0001 |
00000001 | 17/05/2022 09:00 | 0002 |
00000001 | 17/05/2022 09:15 | 0003 |
00000001 | 17/05/2022 12:00 | 0002 |
00000001 | 17/05/2022 13:00 | 0003 |
00000001 | 17/05/2022 15:00 | 0004 |
type 1 is starttime, type 2 is pause, type 3 is continue after pause, type 4 is end_time
I want to calculate the effective shift time, one way to do this in my opinion is to calculate the sum of all start and end times and substract them, for example:
(sum(type 0002) + sum (type 0004)) - (sum (type 0001) + sum (type 0003))
But how to do this in oracle sql?
I've tried:
select shift_id, sum(to_number_to_char(timestamp_,'sssss') where (type_ = 0002 or type is 0004) group by shift_id
Then I get a result like
00000001, 82442
But the sum is not the right sum, it only calculates with seconds after midnight.
Question: How to get a result like:
00000001, 05:45
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您可以使用lag窗口函数来计算时间差,然后将其概括以获取总量-e
href =“ https://dbfiddle.uk/?rdbms=oracle_21& fiddle = 1aea5eee1dfb257b257b27b27b27b27b3219cefd205220522205225225225252525252252525252522525252252522522522522525225252522522522522522522522522522太平洋演示。
You can use LAG window function to calculate the time differencea and thn SUM it to get the total amount -
Demo.
滞后使您一旦拥有当前值和以前的值
,可以根据需要或类似的方式进行间隔
,然后根据需要切片和骰子的方式
Doing a LAG gives you access to the current and previous values
Once you have that, then you can work out intervals as required, eg
or similar depending on how you want to slice and dice