LeetCode-in-Java

3657. Find Loyal Customers

Medium

Table: customer_transactions

+------------------+---------+
| Column Name      | Type    |
+------------------+---------+
| transaction_id   | int     |
| customer_id      | int     |
| transaction_date | date    |
| amount           | decimal |
| transaction_type | varchar |
+------------------+---------+
transaction_id is the unique identifier for this table. transaction_type can be either 'purchase' or 'refund'. 

Write a solution to find loyal customers. A customer is considered loyal if they meet ALL the following criteria:

Return the result table ordered by customer_id in ascending order.

The result format is in the following example.

Example:

Input:

customer_transactions table:

+----------------+-------------+------------------+--------+------------------+
| transaction_id | customer_id | transaction_date | amount | transaction_type |
|----------------|-------------|------------------|--------|------------------|
| 1              | 101         | 2024-01-05       | 150.00 | purchase         |
| 2              | 101         | 2024-01-15       | 200.00 | purchase         |
| 3              | 101         | 2024-02-10       | 180.00 | purchase         |
| 4              | 101         | 2024-02-20       | 250.00 | purchase         |
| 5              | 102         | 2024-01-10       | 100.00 | purchase         |
| 6              | 102         | 2024-01-12       | 120.00 | purchase         |
| 7              | 102         | 2024-01-15       | 80.00  | refund           |
| 8              | 102         | 2024-01-18       | 90.00  | refund           |
| 9              | 102         | 2024-02-15       | 130.00 | purchase         |
| 10             | 103         | 2024-01-01       | 500.00 | purchase         |
| 11             | 103         | 2024-01-02       | 450.00 | purchase         |
| 12             | 103         | 2024-01-03       | 400.00 | purchase         |
| 13             | 104         | 2024-01-01       | 200.00 | purchase         |
| 14             | 104         | 2024-02-01       | 250.00 | purchase         |
| 15             | 104         | 2024-02-15       | 300.00 | purchase         |
| 16             | 104         | 2024-03-01       | 350.00 | purchase         |
| 17             | 104         | 2024-03-10       | 280.00 | purchase         |
| 18             | 104         | 2024-03-15       | 100.00 | refund           |
+----------------+-------------+------------------+--------+------------------+

Output:

+-------------+
| customer_id |
|-------------|
| 101         |
| 104         |
+-------------+

Explanation:

The result table is ordered by customer_id in ascending order.

Solution

# Write your MySQL query statement below
SELECT 
    customer_id
FROM 
    customer_transactions
GROUP BY 
    customer_id
HAVING 
    COUNT(CASE WHEN transaction_type = 'purchase' THEN 1 END) > 2
    AND TIMESTAMPDIFF(DAY, MIN(transaction_date), MAX(transaction_date)) > 29
    AND (COUNT(CASE WHEN transaction_type = 'refund' THEN 1 END) * 1.0 / COUNT(*)) < 0.2
ORDER BY 
    customer_id ASC;