0

我有一张图片中最常见颜色的表格。它看起来像这样:

file | color  | count
---------------------
1    | ffefad | 166
1    | 443834 | 84
2    | 74758a | 3874
2    | abcdef | 228
2    | 876543 | 498
3    | 543432 | 3382
3    | abcdef | 483

我正在尝试为每个图像获取最常见的颜色。所以我希望我的结果是:

file | color  | count
---------------------
1    | ffefad | 166
2    | 74758a | 3874
3    | 543432 | 3382

所以我的问题似乎是我需要 GROUP BYfile列,但 MAX()count列。但简单

SELECT h.file, h.color, MAX(h.count) FROM histogram GROUP BY h.file

不起作用,因为它是不确定的,因此颜色结果与计数结果中的行不匹配。

SELECT h.file, h.color, MAX(h.count) FROM histogram GROUP BY h.file, h.color

修复了确定性,但现在每一行都是“唯一的”并且所有行都被返回。

我想不出一种方法来进行子查询或连接,因为我能想出的唯一“正确”值、文件和计数本身并没有区别。

也许我需要一个更理智的模式?这是“我的”表,所以如果需要我可以更改它。

4

2 回答 2

1
SELECT tbl.file, tbl.color, tbl.count
FROM tbl
LEFT JOIN tbl as lesser
ON lesser.file = tbl.file
AND tbl.count < lesser.count
WHERE lesser.file IS NULL
order by tbl.file
于 2013-09-30T23:23:40.253 回答
1
select file , max(count)
FROM histogram 
GROUP BY h.file

这将按文件给出最大(计数)。将其转换为子查询和内部联接,使其充当过滤器。

select h.file, h.colour, h.count
from histogram inner join
(select file , max(count) as maxcount
FROM histogram 
GROUP BY h.file) a
on a.file = h.file and a.maxcount = h.count

如果有超过 1 种颜色具有相同的最大计数,这将响应 2 行。

于 2013-09-30T23:27:16.563 回答