首页 > 解决方案 > 在 MS ACCESS 中使用 SELF JOIN 对多列求和

问题描述

我有一张桌子 - table1 如下所示:

+------+--------+--------+-----+

ID---hours1---hours2-----hours3--+

1-------4-------3---------2----+

2-------8-------7---------6----+

1-------5-------2---------1----+

2-------10------11--------2----+

预期成绩:

ID-----Total

1--------17

2--------44

我尝试了如下 SELF 连接查询:

SELECT ID, SUM(hours1 + hours2 + hours3) 
from table1 a inner join table1 b ON a.Id = b.Id 
group by a.Id

然而,这给出了不正确的结果,结果非常奇怪。谁能帮我上面的查询有什么问题?

标签: sqlms-access

解决方案


我认为不需要自我加入,只是:

select t.id, sum(t.hours1+t.hours2+t.hours3) as total
from table1 t
group by t.id

推荐阅读