查询以查找过去一周注销时间>6 PM 的所有记录

Query to find all the records where logoff time >6 PM for the past week

我有一个名为 logoninfo 的数据库 Table。以下是架构。

       Record ID|User ID | Computer ID |LOGON_TIME|LOGOFF_TIME

登录和注销时间以毫秒为单位。所有用户每天都会登录,同一用户和计算机将有多个条目,但记录 ID 不同。我需要找到过去一周下午 6 点后注销的所有记录。更具体地说,我需要知道所有过度停留的用户(下午 6 点后注销被视为过度停留)以及他们何时过度停留。请帮我查询。我正在使用 PostgreSQL 9.5。

您需要将可怕的纪元转换为正确的时间戳,然后您可以轻松查询:

select *
from the_table
where 
  -- this selects every row where logged off is after 18:00 
  to_timestamp(logoff_time)::time >  time '18:00'
  -- the following two conditions select all rows from last week
  and to_timesamp(logoff_time) >= date_trunc('week', current_timestamp) - interval '1 week'
  and to_timesamp(logoff_time) < date_trunc('week', current_timestamp) - interval '1 week' + interval '1 week';