1 Star 0 Fork 0

yuhang2__2/LeetCode-Solutions

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
克隆/下载
the-most-frequently-ordered-products-for-each-customer.sql 737 Bytes
一键复制 编辑 原始数据 按行查看 历史
# Time: O(n)
# Space: O(n)
WITH product_count_cte AS
(SELECT c.customer_id,
o.product_id,
p.product_name,
count(c.customer_id) AS product_cnt
FROM Customers c,
Orders o,
Products p
WHERE c.customer_id = o.customer_id
AND o.product_id = p.product_id
GROUP BY c.customer_id,
o.product_id
ORDER BY NULL)
, max_product_count_cte AS
(SELECT customer_id,
max(product_cnt) AS product_cnt
FROM product_count_cte
GROUP BY customer_id
ORDER BY NULL)
SELECT a.customer_id,
a.product_id,
a.product_name
FROM product_count_cte a
INNER JOIN max_product_count_cte b
ON a.customer_id = b.customer_id AND
a.product_cnt = b.product_cnt;
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
1
https://gitee.com/yuhang2__2/LeetCode-Solutions.git
git@gitee.com:yuhang2__2/LeetCode-Solutions.git
yuhang2__2
LeetCode-Solutions
LeetCode-Solutions
master

搜索帮助