Skip to content

Commit 5d1677b

Browse files
authored
Create 1581. Customer Who Visited but Did Not Make Any Transactions.sql
1 parent 08af411 commit 5d1677b

File tree

1 file changed

+83
-0
lines changed

1 file changed

+83
-0
lines changed
Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
/***
2+
Table: Visits
3+
4+
+-------------+---------+
5+
| Column Name | Type |
6+
+-------------+---------+
7+
| visit_id | int |
8+
| customer_id | int |
9+
+-------------+---------+
10+
visit_id is the column with unique values for this table.
11+
This table contains information about the customers who visited the mall.
12+
13+
14+
Table: Transactions
15+
16+
+----------------+---------+
17+
| Column Name | Type |
18+
+----------------+---------+
19+
| transaction_id | int |
20+
| visit_id | int |
21+
| amount | int |
22+
+----------------+---------+
23+
transaction_id is column with unique values for this table.
24+
This table contains information about the transactions made during the visit_id.
25+
26+
27+
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.
28+
29+
Return the result table sorted in any order.
30+
31+
The result format is in the following example.
32+
33+
34+
35+
Example 1:
36+
37+
Input:
38+
Visits
39+
+----------+-------------+
40+
| visit_id | customer_id |
41+
+----------+-------------+
42+
| 1 | 23 |
43+
| 2 | 9 |
44+
| 4 | 30 |
45+
| 5 | 54 |
46+
| 6 | 96 |
47+
| 7 | 54 |
48+
| 8 | 54 |
49+
+----------+-------------+
50+
Transactions
51+
+----------------+----------+--------+
52+
| transaction_id | visit_id | amount |
53+
+----------------+----------+--------+
54+
| 2 | 5 | 310 |
55+
| 3 | 5 | 300 |
56+
| 9 | 5 | 200 |
57+
| 12 | 1 | 910 |
58+
| 13 | 2 | 970 |
59+
+----------------+----------+--------+
60+
Output:
61+
+-------------+----------------+
62+
| customer_id | count_no_trans |
63+
+-------------+----------------+
64+
| 54 | 2 |
65+
| 30 | 1 |
66+
| 96 | 1 |
67+
+-------------+----------------+
68+
Explanation:
69+
Customer with id = 23 visited the mall once and made one transaction during the visit with id = 12.
70+
Customer with id = 9 visited the mall once and made one transaction during the visit with id = 13.
71+
Customer with id = 30 visited the mall once and did not make any transactions.
72+
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.
73+
Customer with id = 96 visited the mall once and did not make any transactions.
74+
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.
75+
76+
***/
77+
78+
SELECT v.customer_id, COUNT(v.visit_id) AS count_no_trans
79+
FROM Visits v
80+
LEFT JOIN Transactions t
81+
ON v.visit_id = t.visit_id
82+
WHERE t.transaction_id IS NULL
83+
GROUP BY v.customer_id

0 commit comments

Comments
 (0)