我需要此查询才能显示最常输入的价格。为此,我需要找到每个提交价格的最大百分比,并将与百分比相关的价格显示为ItemID的可接受值。 我不需要返回百分比。
以下是我将要处理的数据示例:
+--------+-------+
| ItemID | Price |
+--------+-------+
| 12 | 15 |
+--------+-------+
| 12 | 15 |
+--------+-------+
| 12 | 15 |
+--------+-------+
| 14 | 27 |
+--------+-------+
| 14 | 27 |
+--------+-------+
| 12 | 35 |
+--------+-------+
| 12 | 35 |
+--------+-------+
| 14 | 27 |
+--------+-------+
| 12 | 15 |
+--------+-------+
从中得到:
+--------+-------+------------+
| ItemID | Price | Percentage |
+--------+-------+------------+
| 12 | 15 | 66.6% |
+--------+-------+------------+
| 12 | 35 | 33.3% |
+--------+-------+------------+
| 14 | 27 | 100% |
+--------+-------+------------+
最后,这是我输出所需的内容:
所以例如;如果请求了对项目12的查询,请返回:
+--------+-------+
| ItemID | Price |
+--------+-------+
| 12 | 15 |
+--------+-------+
如果要求查询ItemID 14:
+--------+-------+
| ItemID | Price |
+--------+-------+
| 14 | 27 |
+--------+-------+
答案 0 :(得分:1)
尝试以下查询:
SELECT ItemID, Price FROM Table
WHERE ItemID = 12
GROUP BY Price
ORDER BY COUNT(*) DESC
LIMIT 1
输出应为:
+--------+-------+
| ItemID | Price |
+--------+-------+
| 12 | 15 |
+--------+-------+
请注意,您实际上并不需要计算每个价格出现的百分比,因为找到最常出现的价格就足够了。