gpt4 book ai didi

mysql - 两列 SQL 中出现频率最高的值

转载 作者:行者123 更新时间:2023-11-29 05:18:19 24 4
gpt4 key购买 nike

我试图找出分组中表格中出现次数最多的值。这是针对 SQL 的

Part | location | PartDesc
-----+----------+-------------
A | 2 | Part A
A | 2 | Part A
A | 2 | Part A
A | 1 | Part A
A | 1 | Part A
B | 1 | Part B
B | 2 | Part B

所以输出需要显示

Part | Location | PartDesc | Occurrence 
-----+----------+----------+--------------
A | 2 | Part A | 3
A | 1 | Part A | 2
B | 1 | Part B | 1
B | 2 | Part B | 1

目前为止

Select Part, count(*) as occurrence
from table1
group by Part
order by count(*desc)

最佳答案

SELECT 
Part,
Location,
PartDesc,
COUNT(*) AS Occurrence
FROM
table1
GROUP BY
Part,
Location,
PartDesc
ORDER BY
Occurrence DESC

谢谢。

关于mysql - 两列 SQL 中出现频率最高的值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29648004/

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