SQL Query Challenge: Finding the Latest Records (1 Viewer)

bettey12

New member
Local time
Today, 17:42
Joined
Jun 20, 2022
Messages
8
I'm working on a SQL query for a database containing records of customer transactions. Each transaction has a transaction_id, customer_id, transaction_date, and amount. I need to find the latest transaction for each customer.

Here's a simplified version of the table:

SQL:
CREATE TABLE transactions (
    transaction_id INT PRIMARY KEY,
    customer_id INT,
    transaction_date DATE,
    amount DECIMAL(10, 2)
);

INSERT INTO transactions VALUES
    (1, 101, '2023-01-15', 50.00),
    (2, 102, '2023-02-10', 30.00),
    (3, 101, '2023-02-25', 75.00),
    (4, 103, '2023-03-05', 40.00),
    (5, 102, '2023-03-12', 55.00);

I want to write an SQL query that retrieves the latest transaction for each customer. In this example, the expected result would include transactions with transaction_id 3 and 4 because they are the latest for customers 101 and 103, respectively.

Could you provide a SQL query to achieve this? Additionally, it would be helpful if you could explain the logic behind the query and any SQL functions or techniques used. Thank you for your assistance!
 

Minty

AWF VIP
Local time
Today, 13:12
Joined
Jul 26, 2013
Messages
10,371
What happens if a customer has two orders on the same day?
 

ByteMyzer

AWF VIP
Local time
Today, 05:12
Joined
May 3, 2004
Messages
1,409
I'm working on a SQL query for a database containing records of customer transactions. Each transaction has a transaction_id, customer_id, transaction_date, and amount. I need to find the latest transaction for each customer.

Here's a simplified version of the table:

SQL:
CREATE TABLE transactions (
    transaction_id INT PRIMARY KEY,
    customer_id INT,
    transaction_date DATE,
    amount DECIMAL(10, 2)
);

INSERT INTO transactions VALUES
    (1, 101, '2023-01-15', 50.00),
    (2, 102, '2023-02-10', 30.00),
    (3, 101, '2023-02-25', 75.00),
    (4, 103, '2023-03-05', 40.00),
    (5, 102, '2023-03-12', 55.00);

I want to write an SQL query that retrieves the latest transaction for each customer. In this example, the expected result would include transactions with transaction_id 3 and 4 because they are the latest for customers 101 and 103, respectively.

Could you provide a SQL query to achieve this? Additionally, it would be helpful if you could explain the logic behind the query and any SQL functions or techniques used. Thank you for your assistance!

Assuming that field transaction_id is an auto-increment or sequenced (non-repeating value) field, with the highest number being the most recent, you might try something like:

SQL:
SELECT T.*
FROM transactions AS T
INNER JOIN (
    SELECT MAX(transaction_id) AS transaction_idx
    FROM transactions
    GROUP BY customer_id
    ) AS X ON T.transaction_id = X.transaction_idx;
This statement uses an inline subquery to restrict the results to the highest transaction_id for each customer_id.

NOTE: For best performance, the table should have an index for field customer_id, as well as a clustered (primary) index on field transaction_id.
 

Users who are viewing this thread

Top Bottom