PSQL 查询以获取特定月份发布的所有值

PSQL Query to fetch all values posted in a particular month

我正在尝试编写一个查询来获取特定月份的值。我在 psql 将值存储为 bigint 时遇到日期转换问题。我当前的查询在下方,但我必须在查询中明确给出日期。我正在寻找一个更通用的(例如在 gui 中每月 select 并转换为查询)。为此,我只需要比较日期字段的月份和年份(以适应闰年)。 timetocomplete 和 executedtime 是时间戳(bigint)列,我正在尝试计算编号。在执行时间内当月工作的小时数

select sum(s.timetocomplete/1000*1/3600)
from requestdetail s
where
    ('2015-10-31 23:59:59'::timestamp at time zone 'UTC' > TO_TIMESTAMP(executedtime / 1000))
    and
    TO_TIMESTAMP(executedtime / 1000) < '2015-10-31 23:59:59'::timestamp at time zone 'UTC'

您应该使用 epoch() 函数将日期时间转换为秒,然后计算并比较这些值

2015 年 10 月:

with work_month (work_month) as ( values
    ('2015-10-01'::date)
)
select sum(s.timetocomplete / 1000 / 3600)
from requestdetail s cross join work_month
where
    work_month <= to_timestamp(executedtime / 1000)
    and
    to_timestamp(executedtime / 1000) < work_month + interval '1 month'

更简单的版本:

select sum(timetocomplete / 1000 / 3600)
from requestdetail
where date_trunc('month', to_timestamp(executedtime / 1000)) = '2015-10-01'