Сценарий создания данных declare @Employee Table(EmployeeID int ,ActiveStatus nvarchar(20) ,SiteID int)
declare @Site Table(SiteName nvarchar(20) ,SiteID int)
insert into @Employee values(1,'Yes',101),(2,'Yes',101),(3,'Yes',102),(4,'Yes',102),
(5,'Yes',101)
insert into @Site values('Site1',101)
insert into @Site values('Site2',102)
// реальный сценарий для получения процента% ;with cte as
(
select s.SiteName,sum(case when e.ActiveStatus = 'Yes' then 1 else 0 end) as "NumberOfEmployees"
from @Employee e
left join @Site s
on(e.SiteID=s.SiteID)
GROUP BY s.SiteName
),
cte_sum as
(select sum(NumberOfEmployees) as total from cte )
select c.*, convert (decimal(6,2),c.NumberOfEmployees)/convert (decimal(6,2),cs.total)*100 from cte_sum cs, cte c;