【问题标题】:Most frequent values by two columns SQL两列 SQL 中最常见的值
【发布时间】:2015-06-21 06:47:08
【问题描述】:

我试图找出分组表中出现次数最多的值。 这是用于 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)

【问题讨论】:

  • 这个是mysql还是sql server?
  • 你能用一些数据详细解释你想要什么吗?
  • 不清楚的问题,看不懂你想要什么。
  • 希望现在清楚了吗?
  • 您的问题有 2 个赞成票的答案,为什么要删除它?让它存在;)。

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


【解决方案1】:
SELECT 
    Part,
    Location,
    PartDesc,
    COUNT(*) AS Occurrence
FROM
    table1
GROUP BY 
    Part,
    Location,
    PartDesc
ORDER BY 
    Occurrence DESC

谢谢。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2017-03-28
    • 2012-08-27
    • 2010-09-25
    • 2015-06-21
    • 1970-01-01
    • 1970-01-01
    • 2016-11-16
    相关资源
    最近更新 更多