-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy path1581. Customer Who Visited but Did Not Make Any Transactions.sql
More file actions
56 lines (45 loc) · 1.45 KB
/
1581. Customer Who Visited but Did Not Make Any Transactions.sql
File metadata and controls
56 lines (45 loc) · 1.45 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
/*
Question 1581. Customer Who Visited but Did Not Make Any Transactions
Link: https://leetcode.com/problems/customer-who-visited-but-did-not-make-any-transactions/description/?envType=study-plan-v2&envId=top-sql-50
Table: Visits
+-------------+---------+
| Column Name | Type |
+-------------+---------+
| visit_id | int |
| customer_id | int |
+-------------+---------+
visit_id is the column with unique values for this table.
This table contains information about the customers who visited the mall.
Table: Transactions
+----------------+---------+
| Column Name | Type |
+----------------+---------+
| transaction_id | int |
| visit_id | int |
| amount | int |
+----------------+---------+
transaction_id is column with unique values for this table.
This table contains information about the transactions made during the visit_id.
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.
Return the result table sorted in any order.
*/
SELECT
customer_id,
COUNT(visit_id) AS count_no_trans
FROM Visits
WHERE
visit_id NOT IN (
SELECT DISTINCT t.visit_id
FROM Transactions AS t
)
GROUP BY customer_id;
-- OR
SELECT
v.customer_id,
COUNT(v.visit_id) AS count_no_trans
FROM Visits AS v
LEFT JOIN
Transactions AS t
ON v.visit_id = t.visit_id
WHERE t.visit_id IS NULL
GROUP BY v.customer_id