【问题标题】:Query to find all the records where logoff time >6 PM for the past week查询过去一周注销时间>下午6点的所有记录
【发布时间】:2020-08-30 13:26:24
【问题描述】:

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

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

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

【问题讨论】:

  • "登录和注销时间以毫秒为单位" - 几毫秒之后?午夜过后? Unix时代? 4月1日?为什么不是timestamptime 列?
  • @a_horse_with_no_name 这是unix时代。由于内部依赖性,我们不能使用时间戳或时间。上面的查询可以吗?

标签: sql postgresql epoch


【解决方案1】:

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

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';

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2010-11-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多