如何在PostgreSQL视图中获取两个日期时间字段的天数和小时数差异?

如何在PostgreSQL视图中获取两个日期时间字段的天数和小时数差异?

问题描述:

我的数据如下表所示:

  **start_date**                         **end_date**
---------------                      -----------------
2011-06-27 13:24:45.137417          2011-06-28 05:34:26.54939
2011-09-30 09:09:00.501381          2011-10-03 23:38:46.479824

预期结果如下:

Diff
-------
0.16
3.14

其中小数点前的数字是天数和小数点后是小时

where the digits before the decimal point is "Days" & after decimal point is "Hours"

我尝试使用
extract((end_date-start_date)中的'epoch')/(3600 * 24)

i try it using extract('epoch' from (end_date-start_date))/(3600*24)

,但它以100的格式给出小时值,即第一条记录&的0.673395971909722 3.60400438012731(第二条记录)

but it gives the "hours" value in format of 100 i.e 0.673395971909722 for the first record & 3.60400438012731 for second record

请参见文档

SELECT to_char(end_date - start_date, 'D.HH24') AS diff FROM your_table;




0.16
3.14

希望有帮助。