简体   繁体   中英

SUM of multiple columns using SELF JOIN in MS ACCESS

I have a table - table1 like below:

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

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

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

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

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

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

Expected Results:

ID-----Total

1--------17

2--------44

I tried SELF join query as below:

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

However this is giving incorrect results and results are very weird. Can anyone please help me what's wrong in above query?

I don't think the self-join is required for this, just:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM