小编典典

使用函数的SQL查询-如何获取列表的最大数量

sql

如何查询最大交易数…我的代码如下:

SELECT customer_id, COUNT(customer_id)
FROM rental
GROUP BY customer_id
HAVING MAX(COUNT(customer_id)); //I need to get the MAXIMUM COUNT of the list

阅读 269

收藏
2021-04-28

共1个答案

小编典典

由于一个以上的客户可以拥有相同的最大数量,因此您应该执行以下操作

SELECT customer_id, 
       COUNT(customer_id) AS customerrowcount 
FROM   rental 
GROUP  BY customer_id 
HAVING COUNT(customer_id) = (SELECT COUNT(customer_id) 
                             FROM   rental 
                             GROUP  BY customer_id 
                             ORDER  BY COUNT(customer_id) DESC 
                             LIMIT  1)

但是,如果您可以选择任意客户,则应该使用rsbarro的答案

2021-04-28