参照表内的左连接

问题描述

我有一张桌子时间

enter image description here

第二表 SectorProject

enter image description here

第三表 Rfsector

enter image description here

我开发了以下查询

Select sum(time) as time,Sector_L1,T.id_Project
from time T
left join SectorProject SP ON t.id_Project=T.id_Project
left join Rfsector RF on RF.Sector_L2=SP.Sector_L2
GROUP BY Sector_L1,T.id_Project

结果和预期结果

enter image description here

我没看懂结果,谁能解释一下为什么我会得到这个结果,以及如何按 ordr 修改查询以获得预期的结果?

解决方法

您可以使用窗口函数为项目平均分配时间:

select sum(time) as time,Sector_L1,T.id_Project,avg(time) over (partition by t.id_project) as imputed_time
from time T left join
     SectorProject SP 
     on t.id_Project = T.id_Project left join
     Rfsector RF
      on RF.Sector_L2 = SP.Sector_L2
group by Sector_L1,T.id_Project;