Вот полный рабочий пример SQL Server с таблицами, преобразованными в обычные табличные выражения:
with [emp] (eid, ename, age, salary)
as
(
select
eid, ename, age, salary
from
(
values
(1000 , ' Lakmal', 33, 90000),
(1001 , 'Nadeeka', 24, 28000)
) as result (eid, ename, age, salary)
),
[works] (eid, did, pct_time)
as
(
select
eid, did, percentage
from
(
values
(1000, 'Admin', 40),
(1000, 'ITSD', 50),
(1001, 'Admin', 100),
(1002, 'Academic', 100),
(1003, 'Academic', 30)
) as result(eid, did, percentage)
),
[grouped] (eid, pct)
as
(
' Do the grouping here.
select
e.eid, sum(w.pct_time)
from
emp e
inner join
works w on w.eid = e.eid
group by
e.eid
)
' And now you can join the grouped results with the employees to display the names
select
[w].pct as 'Total', e.ename
from
[emp] e
inner join
[grouped] w on w.eid = e.eid;