LeetCode:Database 88.顾客的可信联系人数量

Posted Xiao Miao

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了LeetCode:Database 88.顾客的可信联系人数量相关的知识,希望对你有一定的参考价值。

要求:为每张发票 invoice_id 编写一个SQL查询以查找以下内容:

customer_name:与发票相关的顾客名称。
price:发票的价格。
contacts_cnt:该顾客的联系人数量。
trusted_contacts_cnt:可信联系人的数量:可信联系人的电子邮件存在于客户表中,将查询的结果按照 invoice_id 排序。

顾客表:Customers的结构

+---------------+---------+
| Column Name   | Type    |
+---------------+---------+
| customer_id   | int     |
| customer_name | varchar |
| email         | varchar |
+---------------+---------+
customer_id 是这张表的主键。
此表的每一行包含了某在线商店顾客的姓名和电子邮件。

联系方式表:Contacts的结构

+---------------+---------+
| Column Name   | Type    |
+---------------+---------+
| user_id       | id      |
| contact_name  | varchar |
| contact_email | varchar |
+---------------+---------+
(user_id, contact_email) 是这张表的主键。
此表的每一行表示编号为 user_id 的顾客的某位联系人的姓名和电子邮件。
此表包含每位顾客的联系人信息,但顾客的联系人不一定存在于顾客表中。

发票表:Invoices

+--------------+---------+
| Column Name  | Type    |
+--------------+---------+
| invoice_id   | int     |
| price        | int     |
| user_id      | int     |
+--------------+---------+
invoice_id 是这张表的主键。
此表的每一行分别表示编号为 user_id 的顾客拥有有一张编号为 invoice_id、价格为 price 的发票。

Customers 表:

+-------------+---------------+--------------------+
| 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 表:

+-------------+--------------+--------------------+
| 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 表:

+------------+-------+---------+
| invoice_id | price | user_id |
+------------+-------+---------+
| 77         | 100   | 1       |
| 88         | 200   | 1       |
| 99         | 300   | 2       |
| 66         | 400   | 2       |
| 55         | 500   | 13      |
| 44         | 60    | 6       |
+------------+-------+---------+

Result Table:

+------------+---------------+-------+--------------+----------------------+
| 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                    |
+------------+---------------+-------+--------------+----------------------+
Alice 有三位联系人,其中两位(Bob 和 John)是可信联系人。
Bob 有两位联系人, 他们中的任何一位都不是可信联系人。
Alex 只有一位联系人(Alice),并是一位可信联系人。
John 没有任何联系人。

SQL语句:

#1.原题目的解
with d as( 
select c.customer_name as c1,c.customer_id as c2,b.contact_name as n1,a.customer_id as c3
from customers a
right join contacts b
on a.customer_name=b.contact_name
right join customers c 
on c.customer_id=b.user_id
)

select f.invoice_id,e.c1 as customer_name,f.price,e.cn1 as contacts_cnt,cn2
as trusted_contacts_cnt 
from
(select c1,c2,count(n1) as cn1,count(c3) as cn2
from d 
group by c1)e
join invoices f 
on f.user_id=e.c2
order by f.invoice_id asc;
#2.将条件修改为,可信联系人=该顾客的联系人在顾客表又在发票表中
WITH d AS( 
SELECT c.customer_name AS c1,c.customer_id AS c2,b.contact_name AS n1,a.customer_id AS c3
FROM customers a
RIGHT JOIN contacts b
ON a.customer_name=b.contact_name
RIGHT JOIN customers c 
ON c.customer_id=b.user_id
)

SELECT f.invoice_id,e1.c1 AS customer_name,f.price,e1.cn1 AS contacts_cnt,IFNULL(e2.cn2,0)
AS trusted_contacts_cnt 
FROM
(SELECT c1,c2,COUNT(n1) AS cn1
FROM d 
GROUP BY c1
)e1
LEFT JOIN
(SELECT c1,c2,COUNT(c3) AS cn2
FROM d 
WHERE c3 IN(SELECT user_id FROM invoices)
GROUP BY c1
)e2
ON e1.c1=e2.c1
JOIN invoices f 
ON f.user_id=e1.c2
ORDER BY f.invoice_id ASC;

以上是关于LeetCode:Database 88.顾客的可信联系人数量的主要内容,如果未能解决你的问题,请参考以下文章

LeetCode:Database 114.按月统计订单数与顾客数

LeetCode:Database 114.按月统计订单数与顾客数

LeetCode:Database 114.按月统计订单数与顾客数

LeetCode:Database 93.购买了产品 A 和产品 B 却没有购买产品 C 的顾客

LeetCode:Database 64.即时食物配送 II

LeetCode:Database 63.即时食物配送 I