最常见的值由两列SQL组成

时间:2015-04-15 10:38:20

标签: mysql sql sql-server sql-server-2008 count

我试图找出组中表中最常出现的值。 这适用于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)

1 个答案:

答案 0 :(得分:3)

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

感谢。