ProductId | BrandId | Views
1 | 1 | 3
2 | 1 | 2
3 | 2 | 3
4 | 2 | 4
需要写sql查询才能返回此值:
BrandId | ViewsSummary
1 | 5
2 | 7
请,怎么做?
答案 0 :(得分:3)
这很难“狡猾” - 你只是想用适当的aggregate function来分组你的结果:
SELECT BrandId, SUM(Views) AS ViewsSummary FROM my_table GROUP BY BrandId
在sqlfiddle上查看。