订单最多的客户
在表 orders 中找到订单数最多客户对应的 customer_number 。
数据保证订单数最多的顾客恰好只有一位。
| order_number | customer_number | order_date | required_date | shipped_date | status | comment |
|--------------------|--------------------------|-----------------|--------------------|--------------------|----------|-------------|
| 1 | 1 | 2017-04-09 | 2017-04-13 | 2017-04-12 | Closed | |
| 2 | 2 | 2017-04-15 | 2017-04-20 | 2017-04-18 | Closed | |
| 3 | 3 | 2017-04-16 | 2017-04-25 | 2017-04-20 | Closed | |
| 4 | 3 | 2017-04-18 | 2017-04-28 | 2017-04-25 | Closed | |
顾客3有3,4两个订单,所以输出的顾客编号是3.
方法:
group by 进行顾客分组,
然后count(*)降序排序,
输出顺序为第一个的顾客编号.
SELECT customer_number
FROM orders
GROUP BY customer_number
ORDER BY COUNT(*) DESC
LIMIT 1
本文来自博客园,作者:Widereye,转载请注明原文链接:https://www.cnblogs.com/YH-shjd-senvn/p/15879860.html