繁体   English   中英

Postgresql:如何查找2个日期和2个日期之间的小时数?

[英]Postgresql: How to find hours between 2 dates and 2 times?

Postgresql 9.1

我正在尝试使用查询来查找时间表系统的2个日期和2个之间的小数/小数小时差异。 我正在使用查询来确保软件(不是我编写的)没有任何错误。 以下是我正在使用的表中的字段:startdate是Date字段,starttime是Time字段,enddate是Date字段,endtime是Time字段。

我查看了9.1日期时间文档 ,但仍未找到我需要的内容。

  • age()需要2个时间戳,并且似乎给出整数而不是分数天数的差异。 我认为不可能将age()的结果乘以24来得到小时数。 我也不知道如何在age()函数中包含时间。
  • 我无法找到将日期和时间转换为其他功能以使用其他功能。
  • 我搜索过谷歌和Stackoverflow,但没有找到帮助我的信息。 我已经花了大约4个星期的时间。 所以大概已经30个小时了。
  • 注意:我认为我不能添加用户定义的功能。 我没有权限。

示例数据:

  • 开始日期和时间:'2016-04-29'和'23:00:00'
  • 截止日期和时间:'2016-04-30'和'01:30:00'

我也试过这个sql语句。

SELECT employeetime.dcmasterid as empid, 
       nonchargeabletime.startdate as ncsdate, 
       nonchargeabletime.starttime as ncstime, 
       nonchargeabletime.enddate as ncedate, 
       nonchargeabletime.endtime as ncetime, 
       employeetime.dchours as normhrs, 
       (timestamp (startdate || ' ' || starttime) - timestamp (enddate || ' ' || endtime)) as diffhrs  
FROM employeetime, nonchargeabletime 
WHERE (nonchargeabletime.employeetime=employeetime.dcautoinc) 
       AND  (nonchargeabletime.startdate >= '2016-04-24') 
       AND (nonchargeabletime.startdate <= '2016-04-30') 
       AND (employeetime.dcmasterid IN ('BLURG')) 
       AND (nonchargeabletime.nonchargeabletype=10) 
ORDER BY employeetime.dcmasterid, nonchargeabletime.startdate, nonchargeabletime.starttime;

但是我在startdate遇到语法错误(timestamp (startdate ||

任何人有任何线索如何做到这一点?

谢谢。

date添加time会产生timestamp ,从另一个timestamp减去一个时间戳会返回一个interval

所以你需要做的就是:

(enddate + endtime) - (startdate + starttime) as diff

在SQL的上下文中, interval很好,但通常在编程语言中难以处理。 您可以使用extract(epoch from interval)轻松地将interval转换为秒

如果你想将它转换为小时使用extract并除以3600

extract(epoch from (enddate + endtime) - (startdate + starttime))/3600 as diff_hours

由于您没有字符串,因此无法使用|| 运算符,但您可以添加时间( http://www.postgresql.org/docs/9.1/static/functions-datetime.html )。

这应该工作(如果你想要整数小时,你可以得到结果):

postgres=# create temporary table ts (startdate date, starttime time, enddate date, endtime time);
CREATE TABLE
postgres=# insert into ts values('2016-05-03', '11:45:15', '2016-05-04', '13:55:43');
INSERT 0 1
postgres=# SELECT startdate,starttime,enddate,endtime, (enddate+endtime)-(startdate+starttime) as interval from ts;
 startdate  | starttime |  enddate   | endtime  |    interval    
------------+-----------+------------+----------+----------------
 2016-05-03 | 11:45:15  | 2016-05-04 | 13:55:43 | 1 day 02:10:28
(1 row)

postgres=# SELECT startdate,starttime,enddate,endtime, EXTRACT(epoch FROM ((enddate+endtime)-(startdate+starttime)))/3600 as hours from ts;
 startdate  | starttime |  enddate   | endtime  |      hours       
------------+-----------+------------+----------+------------------
 2016-05-03 | 11:45:15  | 2016-05-04 | 13:55:43 | 26.1744444444444
(1 row)
WITH zooi(startdate,starttime, enddate,endtime) AS (
    VALUES('2016-04-29'::date , '23:00:00'::time
        ,'2016-04-30'::date , '01:30:00'::time )
        )
, stamps (sta, sto) AS (
        select (z.startdate+z.starttime)::timestamp
        , (z.enddate+z.endtime)::timestamp
        FROM zooi z
        )
SELECT sta,sto
        , age(sto,sta) AS how_old
        , (sto-sta)::time AS diff
FROM stamps;

下一步是将时间(或间隔)结果转换为天或小时。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM