Skip to content

Commit d689bcf

Browse files
committed
task: #1581
1 parent bcd1c7c commit d689bcf

File tree

2 files changed

+57
-0
lines changed

2 files changed

+57
-0
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,7 @@ Have a good contributing!
5757
- [1378. Replace Employee ID With The Unique Identifier](./leetcode/easy/1378.%20Replace%20Employee%20ID%20With%20The%20Unique%20Identifier.sql)
5858
- [1407. Top Travellers](./leetcode/easy/1407.%20Top%20Travellers.sql)
5959
- [1484. Group Sold Products By The Date](./leetcode/easy/1484.%20Group%20Sold%20Products%20By%20The%20Date.sql)
60+
- [1581. Customer Who Visited but Did Not Make Any Transactions](./leetcode/easy/1581.%20Customer%20Who%20Visited%20but%20Did%20Not%20Make%20Any%20Transactions.sql)
6061
- [1587. Bank Account Summary II](./leetcode/easy/1587.%20Bank%20Account%20Summary%20II.sql)
6162
- [1633. Percentage of Users Attended a Contest](./leetcode/easy/1633.%20Percentage%20of%20Users%20Attended%20a%20Contest.sql)
6263
- [1683. Invalid Tweets](./leetcode/easy/1683.%20Invalid%20Tweets.sql)
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
/*
2+
Question 1581. Customer Who Visited but Did Not Make Any Transactions
3+
Link: https://leetcode.com/problems/customer-who-visited-but-did-not-make-any-transactions/description/?envType=study-plan-v2&envId=top-sql-50
4+
5+
Table: Visits
6+
7+
+-------------+---------+
8+
| Column Name | Type |
9+
+-------------+---------+
10+
| visit_id | int |
11+
| customer_id | int |
12+
+-------------+---------+
13+
visit_id is the column with unique values for this table.
14+
This table contains information about the customers who visited the mall.
15+
16+
17+
Table: Transactions
18+
19+
+----------------+---------+
20+
| Column Name | Type |
21+
+----------------+---------+
22+
| transaction_id | int |
23+
| visit_id | int |
24+
| amount | int |
25+
+----------------+---------+
26+
transaction_id is column with unique values for this table.
27+
This table contains information about the transactions made during the visit_id.
28+
29+
30+
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.
31+
32+
Return the result table sorted in any order.
33+
*/
34+
35+
SELECT
36+
customer_id,
37+
COUNT(visit_id) AS count_no_trans
38+
FROM Visits
39+
WHERE
40+
visit_id NOT IN (
41+
SELECT DISTINCT t.visit_id
42+
FROM Transactions AS t
43+
)
44+
GROUP BY customer_id;
45+
46+
-- OR
47+
48+
SELECT
49+
v.customer_id,
50+
COUNT(v.visit_id) AS count_no_trans
51+
FROM Visits AS v
52+
LEFT JOIN
53+
Transactions AS t
54+
ON v.visit_id = t.visit_id
55+
WHERE t.visit_id IS NULL
56+
GROUP BY v.customer_id

0 commit comments

Comments
 (0)