-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path1581-CustomerWhoVisitedbutDidNotMakeAnyTransactions.sql
More file actions
96 lines (94 loc) · 3.98 KB
/
1581-CustomerWhoVisitedbutDidNotMakeAnyTransactions.sql
File metadata and controls
96 lines (94 loc) · 3.98 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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
-- 1581. Customer Who Visited but Did Not Make Any Transactions
-- Table: Visits
-- +-------------+---------+
-- | Column Name | Type |
-- +-------------+---------+
-- | visit_id | int |
-- | customer_id | int |
-- +-------------+---------+
-- visit_id is the primary key 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 the primary key for this table.
-- This table contains information about the transactions made during the visit_id.
--
-- Write an SQL query 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.
-- The query result format is in the following example.
--
-- Example 1:
-- Input:
-- Visits
-- +----------+-------------+
-- | visit_id | customer_id |
-- +----------+-------------+
-- | 1 | 23 |
-- | 2 | 9 |
-- | 4 | 30 |
-- | 5 | 54 |
-- | 6 | 96 |
-- | 7 | 54 |
-- | 8 | 54 |
-- +----------+-------------+
-- Transactions
-- +----------------+----------+--------+
-- | transaction_id | visit_id | amount |
-- +----------------+----------+--------+
-- | 2 | 5 | 310 |
-- | 3 | 5 | 300 |
-- | 9 | 5 | 200 |
-- | 12 | 1 | 910 |
-- | 13 | 2 | 970 |
-- +----------------+----------+--------+
-- Output:
-- +-------------+----------------+
-- | customer_id | count_no_trans |
-- +-------------+----------------+
-- | 54 | 2 |
-- | 30 | 1 |
-- | 96 | 1 |
-- +-------------+----------------+
-- Explanation:
-- Customer with id = 23 visited the mall once and made one transaction during the visit with id = 12.
-- Customer with id = 9 visited the mall once and made one transaction during the visit with id = 13.
-- Customer with id = 30 visited the mall once and did not make any transactions.
-- 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.
-- Customer with id = 96 visited the mall once and did not make any transactions.
-- 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.
-- Create table If Not Exists Visits(visit_id int, customer_id int)
-- Create table If Not Exists Transactions(transaction_id int, visit_id int, amount int)
-- Truncate table Visits
-- insert into Visits (visit_id, customer_id) values ('1', '23')
-- insert into Visits (visit_id, customer_id) values ('2', '9')
-- insert into Visits (visit_id, customer_id) values ('4', '30')
-- insert into Visits (visit_id, customer_id) values ('5', '54')
-- insert into Visits (visit_id, customer_id) values ('6', '96')
-- insert into Visits (visit_id, customer_id) values ('7', '54')
-- insert into Visits (visit_id, customer_id) values ('8', '54')
-- Truncate table Transactions
-- insert into Transactions (transaction_id, visit_id, amount) values ('2', '5', '310')
-- insert into Transactions (transaction_id, visit_id, amount) values ('3', '5', '300')
-- insert into Transactions (transaction_id, visit_id, amount) values ('9', '5', '200')
-- insert into Transactions (transaction_id, visit_id, amount) values ('12', '1', '910')
-- insert into Transactions (transaction_id, visit_id, amount) values ('13', '2', '970')
SELECT
customer_id,
count(1) AS count_no_trans
FROM
Visits
WHERE
visit_id NOT IN (
SELECT
visit_id
FROM
Transactions
)
GROUP BY customer_id