Skip to content

Commit 3a62bc5

Browse files
add 1596
1 parent b63d865 commit 3a62bc5

File tree

2 files changed

+14
-0
lines changed

2 files changed

+14
-0
lines changed

README.md

+1
Original file line numberDiff line numberDiff line change
@@ -1096,6 +1096,7 @@ _If you like this project, please leave me a star._ ★
10961096
|1633|[Percentage of Users Attended a Contest](https://leetcode.com/problems/percentage-of-users-attended-a-contest/)|[Solution](../master/database/_1633.sql) || Easy |
10971097
|1623|[All Valid Triplets That Can Represent a Country](https://leetcode.com/problems/all-valid-triplets-that-can-represent-a-country/)|[Solution](../master/database/_1623.sql) || Easy |
10981098
|1607|[Sellers With No Sales](https://leetcode.com/problems/sellers-with-no-sales/)|[Solution](../master/database/_1607.sql) || Easy |
1099+
|1596|[The Most Frequently Ordered Products for Each Customer](https://leetcode.com/problems/the-most-frequently-ordered-products-for-each-customer/)|[Solution](../master/database/_1596.sql) || Medium |
10991100
|1571|[Warehouse Manager](https://leetcode.com/problems/warehouse-manager/)|[Solution](../master/database/_1571.sql) || Easy |
11001101
|1587|[Bank Account Summary II](https://leetcode.com/problems/bank-account-summary-ii/)|[Solution](../master/database/_1587.sql) || Easy |
11011102
|1581|[Customer Who Visited but Did Not Make Any Transactions](https://leetcode.com/problems/customer-who-visited-but-did-not-make-any-transactions/)|[Solution](../master/database/_1581.sql) || Easy |

database/_1596.sql

+13
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
--credit: https://leetcode.com/problems/the-most-frequently-ordered-products-for-each-customer/discuss/861257/simple-and-easy-solution-using-window-function
2+
3+
select customer_id, product_id, product_name from
4+
(
5+
select o.customer_id, o.product_id, p.product_name,
6+
rank() over (partition by customer_id order by count(o.product_id) desc) as ranking
7+
from Orders o
8+
join Products p
9+
on o.product_id = p.product_id
10+
group by customer_id, product_id
11+
) tmp
12+
where ranking = 1
13+
order by customer_id, product_id

0 commit comments

Comments
 (0)