我有以下查询: -
select
dbo.table1.service,
dbo.table1.level_3_structure,
Sum(table1.Reduced) as Total_Reduced
from dbo.table1
where
dbo.table1.Period = 'Cumulative'
Group by
dbo.table1.service,
dbo.table1.level_3_structure
这导致类似于以下内容:-
service level_3_structure Total_Reduced
Service 1 Structure1 11.76
Service 2 Structure2 239.86
Service 3 Structure3 940.29
我有另一个表(表 2),其中包含值 service 和 level_3_structure,还包含一个名为“FTE”的列。
我想要做的是基于服务和 level_3_structure 加入这个表并返回 FTE 的总和。
我尝试了以下查询,但它似乎为每个匹配行重复 table1,导致大约 830 万个结果。
select
dbo.table1.service,
dbo.table1.level_3_structure,
Sum(dbo.table1.Reduced) as Total_Reduced,
Sum(dbo.table2.fte) as 'Total FTE'
from dbo.table1
left join dbo.table2
on dbo.table1.service = dbo.table2.service and
dbo.table1.level_3_structure = dbo.table2.level_3_structure
where
dbo.table1.Period = 'Cumulative'
Group by
dbo.table1.service,
dbo.table1.level_3_structure