Если предположить, что в журналах задач отработаны часы, отработанные разработчиками над проектами, то могут произойти 2 вещи: 1) нет никаких замечаний, что разработчик работал над проектом с участием 2) разработчик работал над проектом, и часы, отработанные им, должны обновлено в вкладе. В обоих случаях подразумевается, что все вклады разработчиков необходимо пересчитывать при каждой вставке в задачи.
Например
drop table if exists t,contribution;
create table t(id int auto_increment primary key, developerid int, projectid int,hrs_normal int, hrs_overtime int);
create table contribution(id int auto_increment primary key, projectid int,
developerid int, hrs int, contribution decimal (6,3) default 0);
drop trigger if exists t;
delimiter $$
create trigger t after insert on t
for each row
begin
declare totalhours int default 0;
set totalhours = (select ifnull(sum(hrs),0) from contribution where projectid = new.projectid);
set totalhours = totalhours + ifnull(new.hrs_normal,0) + ifnull(new.hrs_overtime,0);
if not exists (select 1 from contribution where projectid = new.projectid and developerid = new.developerid) then
insert into contribution(projectid,developerid,hrs)
values
(new.projectid,new.developerid,ifnull(new.hrs_normal,0) + ifnull(new.hrs_overtime,0)
);
else
update contribution
set hrs = hrs + ifnull(new.hrs_normal,0) + ifnull(new.hrs_overtime,0)
where developerid = new.developerid and projectid = new.projectid;
end if;
update contribution
set contribution = (hrs / totalhours) * 100
where projectid = new.projectid;
end $$
delimiter ;
MariaDB [sandbox]> truncate table t;
Query OK, 0 rows affected (0.28 sec)
MariaDB [sandbox]> truncate table contribution;
Query OK, 0 rows affected (0.25 sec)
MariaDB [sandbox]> select * from t;
Empty set (0.00 sec)
MariaDB [sandbox]> select * from contribution;
Empty set (0.00 sec)
MariaDB [sandbox]>
MariaDB [sandbox]> insert into t (developerid , projectid ,hrs_normal , hrs_overtime )
-> values
-> (1,1,10,0);
Query OK, 1 row affected (0.02 sec)
MariaDB [sandbox]>
MariaDB [sandbox]> select * from t;
+----+-------------+-----------+------------+--------------+
| id | developerid | projectid | hrs_normal | hrs_overtime |
+----+-------------+-----------+------------+--------------+
| 1 | 1 | 1 | 10 | 0 |
+----+-------------+-----------+------------+--------------+
1 row in set (0.00 sec)
MariaDB [sandbox]> select * from contribution;
+----+-----------+-------------+------+--------------+
| id | projectid | developerid | hrs | contribution |
+----+-----------+-------------+------+--------------+
| 1 | 1 | 1 | 10 | 100.000 |
+----+-----------+-------------+------+--------------+
1 row in set (0.00 sec)
MariaDB [sandbox]>
MariaDB [sandbox]> insert into t (developerid , projectid ,hrs_normal , hrs_overtime )
-> values
-> (1,1,10,0),(2,1,30,10);
Query OK, 2 rows affected (0.05 sec)
Records: 2 Duplicates: 0 Warnings: 0
MariaDB [sandbox]>
MariaDB [sandbox]> select * from t;
+----+-------------+-----------+------------+--------------+
| id | developerid | projectid | hrs_normal | hrs_overtime |
+----+-------------+-----------+------------+--------------+
| 1 | 1 | 1 | 10 | 0 |
| 2 | 1 | 1 | 10 | 0 |
| 3 | 2 | 1 | 30 | 10 |
+----+-------------+-----------+------------+--------------+
3 rows in set (0.00 sec)
MariaDB [sandbox]> select * from contribution;
+----+-----------+-------------+------+--------------+
| id | projectid | developerid | hrs | contribution |
+----+-----------+-------------+------+--------------+
| 1 | 1 | 1 | 20 | 33.333 |
| 2 | 1 | 2 | 40 | 66.667 |
+----+-----------+-------------+------+--------------+
2 rows in set (0.00 sec)
MariaDB [sandbox]>
MariaDB [sandbox]> insert into t (developerid , projectid ,hrs_normal , hrs_overtime )
-> values
-> (1,1,10,0),(2,2,30,10);
Query OK, 2 rows affected (0.05 sec)
Records: 2 Duplicates: 0 Warnings: 0
MariaDB [sandbox]>
MariaDB [sandbox]> select * from t;
+----+-------------+-----------+------------+--------------+
| id | developerid | projectid | hrs_normal | hrs_overtime |
+----+-------------+-----------+------------+--------------+
| 1 | 1 | 1 | 10 | 0 |
| 2 | 1 | 1 | 10 | 0 |
| 3 | 2 | 1 | 30 | 10 |
| 4 | 1 | 1 | 10 | 0 |
| 5 | 2 | 2 | 30 | 10 |
+----+-------------+-----------+------------+--------------+
5 rows in set (0.00 sec)
MariaDB [sandbox]> select * from contribution;
+----+-----------+-------------+------+--------------+
| id | projectid | developerid | hrs | contribution |
+----+-----------+-------------+------+--------------+
| 1 | 1 | 1 | 30 | 42.857 |
| 2 | 1 | 2 | 40 | 57.143 |
| 3 | 2 | 2 | 40 | 100.000 |
+----+-----------+-------------+------+--------------+
3 rows in set (0.00 sec)
Обратите внимание, если бизнес-правила гласят, что часы разработчика могут быть изменены в задачах, которые вам также понадобятся, и триггер обновления следует в том же духе.