求助:mysql查詢各類產品分別對應銷售記錄最多的銷售員資訊,各產品銷售記錄并列第一的也要列出來
product_name salesman salesman_id
product_001 mark c210001
product_001 tom c210002
product_001 lily c210003
product_001 alex c210004
product_001 mark c210001
product_001 mark c210001
product_001 mark c210001
product_001 tom c210002
product_001 tom c210002
product_001 tom c210002
product_001 alex c210004
product_002 mark c210001
product_002 mark c210001
product_002 mark c210001
product_002 tom c210002
product_002 tom c210002
product_002 tom c210002
product_002 tom c210002
product_002 lily c210003
product_002 lily c210003
product_002 lily c210003
product_002 lily c210003
product_002 lily c210003
product_002 lily c210003
product_002 lily c210003
product_002 alex c210004
product_002 alex c210004
product_002 alex c210004
product_002 alex c210004
uj5u.com熱心網友回復:
select product_name, salesman_id,
DENSE_RANK() OVER (PARTITION BY product_name ORDER BY cnt DESC) AS rk -- 利用視窗函式獲取銷量排名
from (select product_name, salesman_id, COUNT(1) AS cnt
from sales
group by product_name, salesman_id) AS t -- 統計每類產品每個銷售員的銷售記錄數
where rk = 1 -- 選擇第一名,包括并列第一
如果你想學習 SQL,可以參考我的博客。
轉載請註明出處,本文鏈接:https://www.uj5u.com/shujuku/31695.html
標籤:MySQL
上一篇:mysql的in子查詢中有sysdate,導致查詢結果不正確
下一篇:小白求大家給支個招。謝謝了
