|
| 1 | +-- Problem: |
| 2 | + |
| 3 | +/* |
| 4 | +Table: Visits |
| 5 | +
|
| 6 | ++-------------+---------+ |
| 7 | +| Column Name | Type | |
| 8 | ++-------------+---------+ |
| 9 | +| visit_id | int | |
| 10 | +| customer_id | int | |
| 11 | ++-------------+---------+ |
| 12 | +visit_id is the column with unique values for this table. |
| 13 | +This table contains information about the customers who visited the mall. |
| 14 | +
|
| 15 | +
|
| 16 | +Table: Transactions |
| 17 | +
|
| 18 | ++----------------+---------+ |
| 19 | +| Column Name | Type | |
| 20 | ++----------------+---------+ |
| 21 | +| transaction_id | int | |
| 22 | +| visit_id | int | |
| 23 | +| amount | int | |
| 24 | ++----------------+---------+ |
| 25 | +transaction_id is column with unique values for this table. |
| 26 | +This table contains information about the transactions made during the visit_id. |
| 27 | +
|
| 28 | +
|
| 29 | +Write a solution to find the IDs of the users who visited without making any transactions and the number of times they made these types of visits. |
| 30 | +
|
| 31 | +Return the result table sorted in any order. |
| 32 | +
|
| 33 | +The result format is in the following example. |
| 34 | +
|
| 35 | +
|
| 36 | +
|
| 37 | +Example 1: |
| 38 | +
|
| 39 | +Input: |
| 40 | +Visits |
| 41 | ++----------+-------------+ |
| 42 | +| visit_id | customer_id | |
| 43 | ++----------+-------------+ |
| 44 | +| 1 | 23 | |
| 45 | +| 2 | 9 | |
| 46 | +| 4 | 30 | |
| 47 | +| 5 | 54 | |
| 48 | +| 6 | 96 | |
| 49 | +| 7 | 54 | |
| 50 | +| 8 | 54 | |
| 51 | ++----------+-------------+ |
| 52 | +Transactions |
| 53 | ++----------------+----------+--------+ |
| 54 | +| transaction_id | visit_id | amount | |
| 55 | ++----------------+----------+--------+ |
| 56 | +| 2 | 5 | 310 | |
| 57 | +| 3 | 5 | 300 | |
| 58 | +| 9 | 5 | 200 | |
| 59 | +| 12 | 1 | 910 | |
| 60 | +| 13 | 2 | 970 | |
| 61 | ++----------------+----------+--------+ |
| 62 | +Output: |
| 63 | ++-------------+----------------+ |
| 64 | +| customer_id | count_no_trans | |
| 65 | ++-------------+----------------+ |
| 66 | +| 54 | 2 | |
| 67 | +| 30 | 1 | |
| 68 | +| 96 | 1 | |
| 69 | ++-------------+----------------+ |
| 70 | +Explanation: |
| 71 | +Customer with id = 23 visited the mall once and made one transaction during the visit with id = 12. |
| 72 | +Customer with id = 9 visited the mall once and made one transaction during the visit with id = 13. |
| 73 | +Customer with id = 30 visited the mall once and did not make any transactions. |
| 74 | +Customer with id = 54 visited the mall three times. During 2 visits they did not make any transactions, and during one visit they made 3 transactions. |
| 75 | +Customer with id = 96 visited the mall once and did not make any transactions. |
| 76 | +As we can see, users with IDs 30 and 96 visited the mall one time without making any transactions. Also, user 54 visited the mall twice and did not make any transactions. |
| 77 | +*/ |
| 78 | + |
| 79 | +------------------------------------------------------------------------------- |
| 80 | + |
| 81 | +-- Solution: |
| 82 | + |
1 | 83 | SELECT DISTINCT Visits.customer_id, COUNT(Visits.visit_id) AS count_no_trans FROM Visits
|
2 | 84 | LEFT JOIN Transactions ON Visits.visit_id = Transactions.visit_id
|
3 | 85 | WHERE transaction_id IS NULL
|
|
0 commit comments