如何从postgreSQL中的时间戳中减去/添加分钟

jua*_*tto 7 sql postgresql

我有以下场景:

我有员工在工作中登记入住/退房.但他们有10分钟的容忍度.

我从这个观点得到的后期条目:

CREATE OR REPLACE VIEW employees_late_entries
(
  id,
  created_datetime,
  entry_datetime,
  contact_id,
  contact_name,
  user_id,
  employees_perm_id
)
AS 
 SELECT precence_records.id,
    precence_records.created AS created_datetime,
    ("substring"(precence_records.created::text, 0, 11) || ' '::text) || contacts.entry_time::text AS entry_datetime,
    contacts.id AS contact_id,
    contacts.name AS contact_name,
    precence_records.user_id,
    precence_records.employees_perm_id
   FROM precence_records,
    contacts
  WHERE 
    precence_records.type::text = 'entry'::text AND 
    contacts.employee = true AND 
    contacts.id = precence_records.contact_id AND 
    ( ("substring"(precence_records.created::text, 0, 11) || ' '::text) || contacts.entry_time::text) < precence_records.created::text AND 
    precence_records.employees_perm_id IS NULL;
Run Code Online (Sandbox Code Playgroud)

precence_records.created是时候检查和contacts.entry_time时间表输入时间为员工的时间.

这是获得迟到条目的condition contacts.entry_timevs precence_records.created:

 ( ("substring"(precence_records.created::text, 0, 11) || ' '::text) || contacts.entry_time::text) < precence_records.created::text
Run Code Online (Sandbox Code Playgroud)

所以我想做那样的事情:

 (  ("substring"(precence_records.created::text, 0, 11) || ' '::text) || (contacts.entry_time::text + 10 MINUTES)  ) < precence_records.created::text
Run Code Online (Sandbox Code Playgroud)

数据类型:

precence_records.created TIMESTAMP contacts.entry_time VARCHAR

你能帮我吗

Zig*_*ter 12

PostgreSQL中的日期,时间和时间戳可以添加/减去INTERVAL值:

SELECT now()::time - INTERVAL '10 min'
Run Code Online (Sandbox Code Playgroud)

如果您的timestamp字段是varchar,则可以先将其强制转换为时间戳数据类型,然后减去间隔:

 ( (left(precence_records.created::text, 11) || ' ') ||
   (contacts.entry_time::time + INTERVAL '10min')::text )::timestamp <
 precence_records.created::timestamp
Run Code Online (Sandbox Code Playgroud)