如何在PostgreSQL中计算短时和短时?

时间:2018-11-15 07:03:08

标签: sql postgresql

id    checkin   checkout
------------------------
1     12:27      18:10
2     11:00      19:59

我想要此结果并签入时间修复11:00:00 并于20:00:00结帐。

我正在使用此查询

 select min(checktime)::time as checkin, 
        max(checktime)::time as checkout,
        (case 
           when cast(min (a.checktime) as time)  > '11:00:00' then  
               (extract (epoch from min (a.checktime)::time - '11:00:00')/3600 ) 
           else 0  
         end) as shorthour_in ,
         (case 
            when cast(max (a.checktime) as time) <= '20:00:00' 
               then (extract (epoch from  '20:00:00' - max (a.checktime)::time )/3600 ) 
            else 0  
         end) as shorthour_out
from attendance_FHLHR


id    checkin   checkout   shorthour_in     shorthour_out
 ---------------------------------------------------------
  1     12:27      18:10       1.45             1.83
  2     11:00      19:59       0                0.016

任何人都可以指导我获取/计算小时数的正确查询吗?

1 个答案:

答案 0 :(得分:1)

select min (  checktime )::time as checkin, 
max (checktime)::time as checkout,
(case when cast(min (checktime) as time)  >= '11:00:00' then  
(extract (epoch from min (checktime)::time - '11:00:00')/3600 ) else 0  end) as shorthour_in ,
(case when cast(max ( checktime) as time) <= '20:00:00' then  
(extract (epoch from  '20:00:00' - max ( checktime)::time )/3600 ) else 0  end) as shorthour_out
from attendance_FHLHR

检查此答案。