I have the following columns in Table A which records users fingerprint "transaction" every time they check in or check out from a building.
CREATE TABLE user_transactions (
id serial PRIMARY KEY,
staff_id INT4,
transaction_time TIMESTAMP,
transaction_type INT4
);
In a single day a user can have many transactions. How can I create a view that with the following stucture?
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
This one should do the job:
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);