微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

Postgresql创建视图

我在表A中有以下列,它们在每次登记或从建筑物结账时记录用户指纹“交易”.

CREATE TABLE user_transactions(
    id serial PRIMARY KEY,staff_id INT4,transaction_time TIMESTAMP,transaction_type INT4
);

在一天内,用户可以拥有许多交易.如何创建具有以下结构的视图?

staff_id INT4
    transaction_date DATE
    first_transaction TIMESTAMP --first finger scan of the day
    last_transaction TIMESTAMP  --last finger scan of the day
    number_of_transaction INT4  --how many times did the user scan for the day

解决方法

这个应该做的工作:

create or replace view xxx as 
select 
    staff_id,date_trunc('day',transaction_time) transaction_date,min(transaction_time) first_transaction,max(transaction_time) last_transaction,count(*) 
from user_transactions 
group by staff_id,transaction_time);

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。

相关推荐