James Gu
James Gu

Reputation: 1392

SQL get first and last transaction for each user

I have a transaction table with 3 columns: transaction_id, user_id, time_created. I would like to output a table showing:

UserID FirstTransactionTime LastTransactionTime

for each user. What is the best way to construct this query?

Upvotes: 1

Views: 3136

Answers (1)

Zev Spitz
Zev Spitz

Reputation: 15297

Assuming you mean the earliest and latest transaction start time, per user:

SELECT user_id, MIN(time_created), MAX(time_created)
FROM Transactions
GROUP BY user_id

Upvotes: 1

Related Questions