我有一张这样的表:
+----------+-----------+--------+
| BookCode | BranchNum | OnHand |
+----------+-----------+--------+
| 0180 | 1 | 2 |
| 0189 | 2 | 2 |
| 0200 | 1 | 1 |
| 0200 | 2 | 3 |
| 0378 | 3 | 2 |
| 079X | 2 | 1 |
| 079X | 3 | 2 |
| 079X | 4 | 3 |
| 0808 | 2 | 1 |
| 1351 | 2 | 4 |
| 1351 | 3 | 2 |
etc etc
我基本上想为每个BranchNum做一个总和(OnHand),然后对每个BranchNum的总和进行平均。我在这个网站上看到的其他例子似乎有比这更复杂的条件,我只需要使用avg。这样做最简单的方法是什么?
答案 0 :(得分:3)
您实际上可以一步完成:
select sum(onhand) / count(distinct branchnum) as avg_per_branchnum
from atablelikethis;
答案 1 :(得分:2)
select avg(sum_onhand) as avg_onhand
from
(
select branchnum, sum(onhand) as sum_onhand
from your_table
group by branchnum
) tmp_tbl