我需要在一组记录中获取一天中最常见的时刻(黎明、早晨、傍晚、夜晚)。我正在尝试使用 CASE-WHEN 将结果分组,但我无法比较日期和整数。我试试这个:
SELECT done_at,
case done_at
when date_trunc('hour', done_at) > 0 and date_trunc('hour', done_at) <= 7 then
'dawn'
when dayhour > 7 and dayhour <= 12 then
'morning'
when dayhour > 12 and dayhour <= 20 then
'evening'
when dayhour > 20 and dayhour <= 00 then
'night'
end
FROM iterations;
但是得到这个错误:
ERROR: operator does not exist: timestamp without time zone > integer LINE 3: when date_trunc('hour', done_at) > 0 and date_trunc('h...
我也尝试将类型转换为整数,但我得到了这个:
ERROR: cannot cast type timestamp without time zone to integer LINE 3: when date_trunc('hour', done_at)::integer > 0 and date...
问题是它done_at
确实有一个时区。从 Rails 控制台:
?> Iteration.first.done_at
=> Fri, 23 Sep 2011 02:00:00 CEST +02:00
而现在我一无所知。如何获得一天中的时刻?