我试图找出组中表中最常出现的值。 这适用于SQL
标题:部分,位置,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
所以输出需要显示
标题:部分,位置,PartDesc,出现
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)
希望这很清楚。非常感谢
答案 0 :(得分:1)
你越来越近了:
SELECT Part, Location, PartDesc, count(*) as Occurrence
FROM yourtable
GROUP BY Part, Location, PartDesc
ORDER BY Occurence
您希望在GROUP BY
部分中放置要分组的每个字段。实际上,这是您未与Max()
,Sum()
或类似内容聚合的任何字段。如果您想要更复杂的排序,ORDER BY
也可以使用多个字段。