首页 > 其他分享 >1596. 每位顾客最经常订购的商品 - 力扣(LeetCode)

1596. 每位顾客最经常订购的商品 - 力扣(LeetCode)

时间:2024-12-21 23:57:09浏览次数:6  
标签:customer insert product into id 力扣 1596 order LeetCode

  1. 1596. 每位顾客最经常订购的商品 - 力扣(LeetCode)

  2. 目标

    1. 输入

      表:Products
      product_idproduct_nameprice
      1keyboard120
      2mouse80
      3screen600
      4hard disk450
      表:Orders
      order_idorder_datecustomer_idproduct_id
      12020/7/3111
      22020/7/3022
      32020/8/2933
      42020/7/2941
      52020/6/1012
      62020/8/121
      72020/8/133
      82020/8/312
      92020/8/723
      102020/7/1512
      表:Customers
      customer_idname
      1Alice
      2Bob
      3Tom
      4Jerry
      5John
    2. 输出

      输出:
      customer_idproduct_idproduct_name
      21keyboard
      41keyboard
      12mouse
      22mouse
      23screen
      33screen
  3. 分析

    写一个解决方案,找到每一个顾客最经常订购的商品。
    结果表单应该有每一位至少下过一次单的顾客 customer_id , 他最经常订购的商品的 product_id 和 product_name。
    表:Products表:Orders表:Customers输出:
    product_idproduct_namepriceorder_idorder_datecustomer_idproduct_idcustomer_idnamecustomer_idproduct_idproduct_name
    1keyboard12012020/7/31111Alice21keyboard
    2mouse8022020/7/30222Bob41keyboard
    3screen60032020/8/29333Tom12mouse
    4hard disk45042020/7/29414Jerry22mouse
    52020/6/10125John23screen
    62020/8/12133screen
    72020/8/133
    82020/8/312
    92020/8/723
    102020/7/1512
    分组计算每个顾客订购每个商品的次数customer_idproduct_idcnt每个顾客订购每个商品的次数排序customer_idproduct_idcntrn查询用户订购最多次数的商品id和namecustomer_idproduct_idproduct_name
    111123121keyboard
    221111241keyboard
    332221112mouse
    411211122mouse
    123231123screen
    211332133screen
    2314111
  4. 实现

    DROP TABLE IF EXISTS Customers;
    DROP TABLE IF EXISTS Orders;
    DROP TABLE IF EXISTS Products;
    Create table If Not Exists Customers (customer_id int, name varchar(10));
    Create table If Not Exists Orders (order_id int, order_date date, customer_id int, product_id int);
    Create table If Not Exists Products (product_id int, product_name varchar(20), price int);
    Truncate table Customers;
    insert into Customers (customer_id, name) values ('1', 'Alice');
    insert into Customers (customer_id, name) values ('2', 'Bob');
    insert into Customers (customer_id, name) values ('3', 'Tom');
    insert into Customers (customer_id, name) values ('4', 'Jerry');
    insert into Customers (customer_id, name) values ('5', 'John');
    Truncate table Orders;
    insert into Orders (order_id, order_date, customer_id, product_id) values ('1', '2020-07-31', '1', '1');
    insert into Orders (order_id, order_date, customer_id, product_id) values ('2', '2020-7-30', '2', '2');
    insert into Orders (order_id, order_date, customer_id, product_id) values ('3', '2020-08-29', '3', '3');
    insert into Orders (order_id, order_date, customer_id, product_id) values ('4', '2020-07-29', '4', '1');
    insert into Orders (order_id, order_date, customer_id, product_id) values ('5', '2020-06-10', '1', '2');
    insert into Orders (order_id, order_date, customer_id, product_id) values ('6', '2020-08-01', '2', '1');
    insert into Orders (order_id, order_date, customer_id, product_id) values ('7', '2020-08-01', '3', '3');
    insert into Orders (order_id, order_date, customer_id, product_id) values ('8', '2020-08-03', '1', '2');
    insert into Orders (order_id, order_date, customer_id, product_id) values ('9', '2020-08-07', '2', '3');
    insert into Orders (order_id, order_date, customer_id, product_id) values ('10', '2020-07-15', '1', '2');
    Truncate table Products;
    insert into Products (product_id, product_name, price) values ('1', 'keyboard', '120');
    insert into Products (product_id, product_name, price) values ('2', 'mouse', '80');
    insert into Products (product_id, product_name, price) values ('3', 'screen', '600');
    insert into Products (product_id, product_name, price) values ('4', 'hard disk', '450');
    SELECT *
    FROM Products;
    SELECT *
    FROM Orders;
    SELECT *
    FROM Customers;
    # 分组计算每个顾客订购每个商品的次数
    SELECT customer_id,product_id,count(*)cnt
    FROM Orders
    GROUP BY customer_id, product_id;
    # 每个顾客订购每个商品的次数排序
    WITH t1 AS ( SELECT customer_id,product_id,count(*)cnt
    FROM Orders
    GROUP BY customer_id, product_id )
    SELECT customer_id, product_id,cnt,rank() OVER (PARTITION BY customer_id ORDER BY cnt DESC )rn
    FROM t1;
    # 查询用户订购最多次数的商品id和name
    WITH t1 AS ( SELECT customer_id,product_id,count(*)cnt
    FROM Orders
    GROUP BY customer_id, product_id ),
    t2 AS ( SELECT customer_id, product_id,cnt,rank() OVER (PARTITION BY customer_id ORDER BY cnt DESC )rn
    FROM t1 )
    SELECT customer_id,p.product_id,product_name
    FROM t2,products p
    WHERE rn=1 AND t2.product_id=p.product_id;

  5. 小结

        rank排序窗口,子查询

标签:customer,insert,product,into,id,力扣,1596,order,LeetCode
From: https://blog.csdn.net/2301_78665414/article/details/144638186

相关文章

  • 【多维DP】【准NOI难度】力扣3251. 单调数组对的数目 II
    给你一个长度为n的正整数数组nums。如果两个非负整数数组(arr1,arr2)满足以下条件,我们称它们是单调数组对:两个数组的长度都是n。arr1是单调非递减的,换句话说arr1[0]<=arr1[1]<=…<=arr1[n-1]。arr2是单调非递增的,换句话说arr2[0]>=ar......
  • LeetCode72. 编辑距离(2024冬季每日一题 37)
    给你两个单词word1和word2,请返回将word1转换成word2所使用的最少操作数。你可以对一个单词进行如下三种操作:插入一个字符删除一个字符替换一个字符示例1:输入:word1=“horse”,word2=“ros”输出:3解释:horse->rorse(将‘h’替换为‘r’)rorse->......
  • LeetCode 热题 第17题 电话号码的字母组合
    LeetCode热题17电话号码的字母组合给定一个仅包含数字2-9的字符串,返回所有它能表示的字母组合。答案可以按任意顺序返回。给出数字到字母的映射如下(与电话按键相同)。注意1不对应任何字母。解答classSolution{public:vector<string>letterCombinations......
  • LeetCode - #166 分数到小数
    文章目录前言1.描述2.示例3.答案关于我们前言我们社区陆续会将顾毅(Netflix增长黑客,《iOS面试之道》作者,ACE职业健身教练。)的Swift算法题题解整理为文字版以方便大家学习与阅读。LeetCode算法到目前我们已经更新到163期,我们会保持更新时间和进度(周一、......
  • 12.16 二叉树的题目用acm模式 leetcode
    任务有leetcode1.将所有二叉树的题目用acm模式进行补充(完成了)github上面的所有二叉树ACM答案,模板https://github.com/PUNKDONG/leetcode/tree/master/src/treenodepackagetreenode;importjava.util.*;publicclasstreecode0_template{staticclassTreeNo......
  • 24/12/20随笔:记录一下每日力扣看到的modern c++
    3138.同位字符串连接的最小长度给你一个字符串s,它由某个字符串t和若干t的同位字符串连接而成。请你返回字符串t的最小可能长度。同位字符串指的是重新排列一个单词得到的另外一个字符串,原来字符串中的每个字符在新字符串中都恰好只使用一次。示例1:输入:s="a......
  • leetcode 2592. 最大化数组的伟大值
    2592.最大化数组的伟大值法一:排序丑陋的代码classSolution{public:intmaximizeGreatness(vector<int>&nums){sort(nums.begin(),nums.end());intsize=nums.size(),res=0;for(inti=0,j=0;i<size&&j<size;+......
  • leetcode 8. 字符串转换整数 (atoi)
    8.字符串转换整数(atoi)丑陋的代码classSolution{public:intmyAtoi(strings){inti=0,size=s.size();boolisPositive=true;longres=0;while(s[i]=='')++i;//跳过空格if(s[i]=='-'){......
  • LeetCode 热题 第35题 搜索插入位置
    LeetCode热题第35题搜索插入位置给定一个排序数组和一个目标值,在数组中找到目标值,并返回其索引。如果目标值不存在于数组中,返回它将会被按顺序插入的位置。请必须使用时间复杂度为O(logn)的算法。解题:classSolution{public:intsearchInsert(vector<int>&......
  • LeetCode题集-9 - 回文数
    题目:给你一个整数 x ,如果 x 是一个回文整数,返回 true ;否则,返回 false 。回文数是指正序(从左向右)和倒序(从右向左)读都是一样的整数。例如,121 是回文,而 123 不是。01、反转字符串法此题我第一反应就是直接把整数转为字符串,然后通过字符串Reverse方法,反转字符串,最后......