Find centralized, trusted content and collaborate around the technologies you use most.
Teams
Q&A for work
Connect and share knowledge within a single location that is structured and easy to search.
我有一个包含 100 行和两列(颜色、成本)的表。
现在总共 100 行包含 5 种颜色(蓝色、黑色、黄色、橙色、绿色)。
那么,现在如果我想按颜色取平均成本,我该如何在 SQL 中做到这一点?
我的意思是,我的决赛桌应该是
color | avgcost --------+-------- blue | 120 black | 80 yellow | 77 orange | 84 green | 44
使用GROUP BY子句对颜色进行分组并AVG计算每组的平均值:
GROUP BY
AVG
SELECT color, AVG(cost) AS avgcost FROM T GROUP BY color;
请查看此演示。