Skip to content

Commit c1c7262

Browse files
committed
Sync LeetCode submission - Customer Who Visited but Did Not Make Any Transactions - Runtime - 457 ms (80.73%), Memory - 0.0B (100.00%)
1 parent a12b8e3 commit c1c7262

2 files changed

Lines changed: 86 additions & 0 deletions

File tree

Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
<p>Table: <code>Visits</code></p>
2+
3+
<pre>
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+
</pre>
13+
14+
<p>&nbsp;</p>
15+
16+
<p>Table: <code>Transactions</code></p>
17+
18+
<pre>
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+
</pre>
29+
30+
<p>&nbsp;</p>
31+
32+
<p>Write a&nbsp;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.</p>
33+
34+
<p>Return the result table sorted in <strong>any order</strong>.</p>
35+
36+
<p>The&nbsp;result format is in the following example.</p>
37+
38+
<p>&nbsp;</p>
39+
<p><strong class="example">Example 1:</strong></p>
40+
41+
<pre>
42+
<strong>Input:</strong>
43+
Visits
44+
+----------+-------------+
45+
| visit_id | customer_id |
46+
+----------+-------------+
47+
| 1 | 23 |
48+
| 2 | 9 |
49+
| 4 | 30 |
50+
| 5 | 54 |
51+
| 6 | 96 |
52+
| 7 | 54 |
53+
| 8 | 54 |
54+
+----------+-------------+
55+
Transactions
56+
+----------------+----------+--------+
57+
| transaction_id | visit_id | amount |
58+
+----------------+----------+--------+
59+
| 2 | 5 | 310 |
60+
| 3 | 5 | 300 |
61+
| 9 | 5 | 200 |
62+
| 12 | 1 | 910 |
63+
| 13 | 2 | 970 |
64+
+----------------+----------+--------+
65+
<strong>Output:</strong>
66+
+-------------+----------------+
67+
| customer_id | count_no_trans |
68+
+-------------+----------------+
69+
| 54 | 2 |
70+
| 30 | 1 |
71+
| 96 | 1 |
72+
+-------------+----------------+
73+
<strong>Explanation:</strong>
74+
Customer with id = 23 visited the mall once and made one transaction during the visit with id = 12.
75+
Customer with id = 9 visited the mall once and made one transaction during the visit with id = 13.
76+
Customer with id = 30 visited the mall once and did not make any transactions.
77+
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.
78+
Customer with id = 96 visited the mall once and did not make any transactions.
79+
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.
80+
</pre>
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
-- Write your PostgreSQL query statement below
2+
SELECT Visits.customer_id, COUNT(Visits.visit_id) AS count_no_trans
3+
FROM Visits
4+
LEFT JOIN Transactions ON Visits.visit_id = Transactions.visit_id
5+
WHERE Transactions.transaction_id IS NULL
6+
GROUP BY Visits.customer_id;

0 commit comments

Comments
 (0)