返回介绍

solution / 1300-1399 / 1364.Number of Trusted Contacts of a Customer / README_EN

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

1364. Number of Trusted Contacts of a Customer

中文文档

Description

Table: Customers

+---------------+---------+
| Column Name   | Type  |
+---------------+---------+
| customer_id   | int   |
| customer_name | varchar |
| email     | varchar |
+---------------+---------+
customer_id is the column of unique values for this table.
Each row of this table contains the name and the email of a customer of an online shop.

 

Table: Contacts

+---------------+---------+
| Column Name   | Type  |
+---------------+---------+
| user_id     | id    |
| contact_name  | varchar |
| contact_email | varchar |
+---------------+---------+
(user_id, contact_email) is the primary key (combination of columns with unique values) for this table.
Each row of this table contains the name and email of one contact of customer with user_id.
This table contains information about people each customer trust. The contact may or may not exist in the Customers table.

 

Table: Invoices

+--------------+---------+
| Column Name  | Type  |
+--------------+---------+
| invoice_id   | int   |
| price    | int   |
| user_id    | int   |
+--------------+---------+
invoice_id is the column of unique values for this table.
Each row of this table indicates that user_id has an invoice with invoice_id and a price.

 

Write a solution to find the following for each invoice_id:

  • customer_name: The name of the customer the invoice is related to.
  • price: The price of the invoice.
  • contacts_cnt: The number of contacts related to the customer.
  • trusted_contacts_cnt: The number of contacts related to the customer and at the same time they are customers to the shop. (i.e their email exists in the Customers table.)

Return the result table ordered by invoice_id.

The result format is in the following example.

 

Example 1:

Input: 
Customers table:
+-------------+---------------+--------------------+
| customer_id | customer_name | email        |
+-------------+---------------+--------------------+
| 1       | Alice     | alice@leetcode.com |
| 2       | Bob       | bob@leetcode.com   |
| 13      | John      | john@leetcode.com  |
| 6       | Alex      | alex@leetcode.com  |
+-------------+---------------+--------------------+
Contacts table:
+-------------+--------------+--------------------+
| user_id   | contact_name | contact_email    |
+-------------+--------------+--------------------+
| 1       | Bob      | bob@leetcode.com   |
| 1       | John     | john@leetcode.com  |
| 1       | Jal      | jal@leetcode.com   |
| 2       | Omar     | omar@leetcode.com  |
| 2       | Meir     | meir@leetcode.com  |
| 6       | Alice    | alice@leetcode.com |
+-------------+--------------+--------------------+
Invoices table:
+------------+-------+---------+
| invoice_id | price | user_id |
+------------+-------+---------+
| 77     | 100   | 1     |
| 88     | 200   | 1     |
| 99     | 300   | 2     |
| 66     | 400   | 2     |
| 55     | 500   | 13    |
| 44     | 60  | 6     |
+------------+-------+---------+
Output: 
+------------+---------------+-------+--------------+----------------------+
| invoice_id | customer_name | price | contacts_cnt | trusted_contacts_cnt |
+------------+---------------+-------+--------------+----------------------+
| 44     | Alex      | 60  | 1      | 1          |
| 55     | John      | 500   | 0      | 0          |
| 66     | Bob       | 400   | 2      | 0          |
| 77     | Alice     | 100   | 3      | 2          |
| 88     | Alice     | 200   | 3      | 2          |
| 99     | Bob       | 300   | 2      | 0          |
+------------+---------------+-------+--------------+----------------------+
Explanation: 
Alice has three contacts, two of them are trusted contacts (Bob and John).
Bob has two contacts, none of them is a trusted contact.
Alex has one contact and it is a trusted contact (Alice).
John doesn't have any contacts.

Solutions

Solution 1

# Write your MySQL query statement below
SELECT
  invoice_id,
  t2.customer_name,
  price,
  COUNT(t3.user_id) AS contacts_cnt,
  COUNT(t4.email) AS trusted_contacts_cnt
FROM
  Invoices AS t1
  LEFT JOIN Customers AS t2 ON t1.user_id = t2.customer_id
  LEFT JOIN Contacts AS t3 ON t1.user_id = t3.user_id
  LEFT JOIN Customers AS t4 ON t3.contact_email = t4.email
GROUP BY invoice_id
ORDER BY invoice_id;

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

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

发布评论

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