gpt4 book ai didi

mysql - 获取mysql中重复值的多个最大计数

转载 作者:行者123 更新时间:2023-11-29 13:07:19 25 4
gpt4 key购买 nike

如何从表中获取重复值最大计数的输出,该表包含与列相对应的重复值,这样就有多个不同的不同值具有最大计数。

考虑下面的表格数据:

+---------+------------+-------------+--------------+
| Bill_No | Bill_Date | Customer_ID | Total_Amount |
+---------+------------+-------------+--------------+
| 101 | 2012-04-10 | C001 | 64 |
| 102 | 2012-04-10 | C002 | 8 |
| 103 | 2012-04-11 | C002 | 140 |
| 104 | 2012-04-13 | C001 | 29 |
| 105 | 2012-04-12 | C003 | 125 |
| 106 | 2012-04-16 | C004 | 258 |
+---------+------------+-------------+--------------+

我们在这里看到 C001C002 的最大 count(customer_id) 是相同的。我想获得这两个值。

最终输出应如下所示:

customer_id     |    count(customer_id)  //max value
----------------+-----------------------
C001 | 2
C002 | 2
----------------+-----------------------

最佳答案

所以,经过一些猜测,会

select
distinct g.customer_id,
g.cnt
from
(
select
distinct customer_id,
count(customer_id) cnt
from
table
group by
customer_id
) g
inner join
(
select
max(s.cnt) max_cnt
from
(
select
distinct customer_id,
count(customer_id) cnt
from
table
group by
customer_id
) s
) m
on
m.max_cnt = g.cnt

有技巧吗?

关于mysql - 获取mysql中重复值的多个最大计数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22530103/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com