Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/311.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何在PostgreSQL中相互减去小时和分钟_Sql_Postgresql - Fatal编程技术网

如何在PostgreSQL中相互减去小时和分钟

如何在PostgreSQL中相互减去小时和分钟,sql,postgresql,Sql,Postgresql,我有两个字段dates和closingTime dates是一个普通的时间戳字段(例如2019-07-13 22:31:10.000000) closingTime是商店关门时的HH:MM格式(例如23:00) 我需要一个PostgreSQL查询来减去这两个字段,并得到它们之间的分钟数差 使用上述示例,两个字段之间的差值为28分钟 到目前为止,我已经尝试了datediff函数的不同变体,但它不起作用 我猜我要么必须 a。为closingTime生成假时间戳,该时间戳与dates字段的日期相

我有两个字段
dates
closingTime

  • dates
    是一个普通的时间戳字段(例如2019-07-13 22:31:10.000000)
  • closingTime
    是商店关门时的HH:MM格式(例如23:00)
我需要一个PostgreSQL查询来减去这两个字段,并得到它们之间的分钟数差

使用上述示例,两个字段之间的差值为28分钟

到目前为止,我已经尝试了datediff函数的不同变体,但它不起作用

我猜我要么必须

  • a。为
    closingTime
    生成假时间戳,该时间戳与
    dates
    字段的日期相同,并减去2个时间戳

  • b。将这两个字段的小时/分钟转换为浮点值,然后减去这两个值以获得小时差并将其转换为分钟
您可以通过将时间戳转换为时间来调整它们:

select closingtime - datets::time
from your_table;
这将给你一个结果

要将其转换为分钟,您可以获得秒数并将其除以60:

select (extract epoch from closingtime - datets::time) / 60
from your_table;

将结束时间转换为间隔,时间戳转换为时间,然后减去两者。通过将时间戳转换为time,实际上就是丢弃了日期部分。您可以将其中一个减去另一个,以生成作为间隔的差值

select closingTime::interval - dateTS::time...
e、 g:

如果需要,您可以将时间间隔转换为分钟:

# select extract(epoch from ('23:00'::interval - now()::time)) / 60;
     ?column?
------------------
 327.435313083333
(1 row)

奇怪的是,我在选择
dateTS::time
:[0A000][500310][Amazon](500310)时遇到此红移错误。无效操作:红移表不支持指定的类型或函数(每个信息消息一个)。;无论如何,我已经在
dates
上使用了substring函数来获取HH:MM,并使用了interval,现在它可以工作了<代码>提取(历元自(closingTime::interval子字符串(dates,12,5)::interval))/60
# select extract(epoch from ('23:00'::interval - now()::time)) / 60;
     ?column?
------------------
 327.435313083333
(1 row)