15. Visitors Without Transactions
A shopping mall management system tracks customer visits and transactions. Some customers visit the mall but do not make any purchases during their visit.
The mallβs management wants to identify these customers and determine how many times they visited without making a transaction.
Your task is to find all customers who visited without making any transactions and count the number of such visits per customer.
The company maintains two tables:
Visits table, which contains:
βββββββββββββββ¦βββββββββββ β Column Name β Type β β ββββββββββββββ¬βββββββββββ£ β visit_id β int β βββββββββββββββΌβββββββββββ β customer_id β int β βββββββββββββββ©βββββββββββ
- visit_id: A unique identifier for each visit.
- customer_id: The ID of the customer who visited the mall.
Transactions table, which contains:
ββββββββββββββββββ¦βββββββββββ β Column Name β Type β β βββββββββββββββββ¬βββββββββββ£ β transaction_id β int β ββββββββββββββββββΌβββββββββββ β visit_id β int β ββββββββββββββββββΌβββββββββββ β amount β int β ββββββββββββββββββ©βββββββββββ
- transaction_id: A unique identifier for each transaction.
- visit_id: A reference to the visit in which the transaction occurred.
- amount: The amount spent during the transaction.
Your task is to:
- Find customers who visited but did not make any transactions.
- Count how many times each customer made such visits.
Return the result in sorted by count_no_trans in descending order.
The sample output format is in the following example.
Example 1:
Example Input:
Visits Table
ββββββββββββ¦ββββββββββββββ β visit_id β customer_id β β βββββββββββ¬ββββββββββββββ£ β 1 β 23 β ββββββββββββΌββββββββββββββ β 2 β 9 β ββββββββββββΌββββββββββββββ β 4 β 30 β ββββββββββββΌββββββββββββββ β 5 β 54 β ββββββββββββΌββββββββββββββ β 6 β 96 β ββββββββββββΌββββββββββββββ β 7 β 54 β ββββββββββββΌββββββββββββββ β 8 β 54 β ββββββββββββ©ββββββββββββββ
Transactions Table
ββββββββββββββββββ¦βββββββββββ¦βββββββββββ β 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 23 - Made a transaction during visit 1, Excluded
- Customer 9 - Made a transaction during visit 2, Excluded
- Customer 30 - Visited once (visit 4) and did not make a transaction, Included
- Customer 54 - Visited 3 times (visits 5, 7, 8) but made transactions only in visit 5 , Visited twice without a transaction , Included with count 2
- Customer 96 - Visited once (visit 6) and did not make a transaction , Included
Still unsure what the problem is asking ?
Letβs go through a few more examples, step by step, to make it clearer.