返回介绍

solution / 1500-1599 / 1581.Customer Who Visited but Did Not Make Any Transactions / README_EN

发布于 2024-06-17 01:03:17 字数 4326 浏览 0 评论 0 收藏 0

1581. Customer Who Visited but Did Not Make Any Transactions

中文文档

Description

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.

The 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.

Solutions

Solution 1: Subquery + Grouping

We can use a subquery to first find all visit_ids that have not made any transactions, and then group by customer_id to count the number of times each customer has not made any transactions.

# Write your MySQL query statement below
SELECT customer_id, COUNT(1) AS count_no_trans
FROM Visits
WHERE visit_id NOT IN (SELECT visit_id FROM Transactions)
GROUP BY 1;

Solution 2: Left Join + Grouping

We can also use a left join to join the Visits table and the Transactions table on visit_id, and then filter out the records where amount is NULL. After that, we can group by customer_id to count the number of times each customer has not made any transactions.

# Write your MySQL query statement below
SELECT customer_id, COUNT(1) AS count_no_trans
FROM
  Visits
  LEFT JOIN Transactions USING (visit_id)
WHERE amount IS NULL
GROUP BY 1;

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
    我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
    原文