Postgresql创建视图

Ran*_*Joe 4 sql postgresql group-by views aggregate-functions

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

CREATE TABLE user_transactions(
    id serial PRIMARY KEY,
    staff_id INT4,
    transaction_time TIMESTAMP,
    transaction_type INT4
);
Run Code Online (Sandbox Code Playgroud)

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

    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
Run Code Online (Sandbox Code Playgroud)

A.H*_*.H. 20

这个应该做的工作:

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, date_trunc('day', transaction_time);
Run Code Online (Sandbox Code Playgroud)