我有以下medleys
表,结合colors
, fruits
和ratings
:
[medleys]
medley_id | color | fruit | rating
==============================================
1 red apple 25
2 blue pear 5
3 green apple 12
4 red apple 10
5 purple kiwi 5
6 purple kiwi 50
7 blue kiwi 3
8 blue pear 9
我试图写一个符合ANSI-SQL查询,将结合每一个独特/不同的color
- fruit
对和总结每一对的个人rating
值。 因此,如果你跑表上面的查询会产生以下结果集:
[query]
color | fruit | sum
===========================
red apple 35
blue pear 14
blue kiwi 3
green apple 12
purple kiwi 55
因此,查询看到有两个red
- apple
在表中对,并且因此它创建一个结果为red
- apple
对,并且增加了它们的组成ratings
(25 + 10 = 35)等。
我相信,我需要做一个选择的独特的颜色/果值,但不知道如何在相同的“水平/范围”聚集评级:
SELECT
distinct(color, fruit), sum(rating)
FROM
medleys
顺序并不重要。 color
和fruit
是VARCHAR(50)S和rating
是INT。 提前致谢!