如何使用PostgreSQL从两个单独的时间测量间隔时间

时间:2019-03-28 16:50:44

标签: postgresql time intervals

我有一些字段分别是pid(主键),login,logout,interval_time,今天(current_date)

登录和注销数据类型为(无时区的时间)

这里的登录和注销数据数据例如10:00:45、12:45:12、07:32:02(不像14:32:12、18:65:78、23:51:00)< / p>

所以我不知道是晚上还是白天,或者是下午还是上午。

If someone login = 07:00:00 and logout = 11:00:00
again he/she login = 01:00:00
that means here interval_time will be 2 hours. Exactly how much time he/she was inactive.

我写了一个触发器

create or replace function interval_time()
returns trigger as
$BODY$
    begin
        if new.login or new.logout then
            if new.login then
                update employee set interval_time = new.logout::time - login::time where pid = new.pid; -- employee is my table name;
                return new;
            end if;
        end if;
    end;
$BODY$
language plpgsql;

此触发器只是示例,无法正常工作。

create trigger employee_changer
before update
on employee
for each row
execute procedure employee_change()

Please let me know if you have any question.
Any help would be appreciated. 
Thanks.

0 个答案:

没有答案