大家好。 我有一张DB,如下图所示。 enter image description here
每个季节,每个人员都可以有一个或多个帖子,每个帖子都在一个特定的单元中。我的问题是如何计算每个相关职位中每个人员的加权AVG(请注意,我们有两种类型的标准,其中ct.ID = 1或2)。我做了如下的事情
select r.EvaluaTEDID,r.PostID, Avg(r.FirstScore*iif(ct.id=1,p.COMCrtWeight/100,0))+
Avg(r.FirstScore*iif(ct.id=2,p.PROCrtWeight/100,0)) as 'AVG'
from results r
inner join post p on r.PostID = p.ID
inner join job j on p.JobID = j.ID
inner join subcrt sc on r.SubCrtID = sc.ID
inner join Crt c on sc.CrtID = c.ID
inner join CrtType ct on c.CrtTypeID = ct.ID
where r.SeasonID = 4 and j.UnitID = 3068
group by r.EvaluaTEDID, r.PostID
但MSSQLS显示46,这是ID = 8的人员的所有记录的加权平均值。期望的答案应该是87(36 + 51)
COMCrtWeight = 40,PROCrtWeight = 60
SubCID = 6的ct.ID是2,而SubCID的ct.ID = 1& 4是1seasonID - EvaluaTEDID - SubCID - PostID - 得分
4 ------------ 8 ------------------ 6 ----------- 26 - ---- 90
4 ------------ 8 ------------------ 1 ----------- 26 - ---- 90
4 ------------ 8 ------------------ 4 ----------- 26 - ---- 90
我也做过这样的事情:
select EvaluaTEDID, sum(WeightedSum) from
(select r.EvaluaTEDID,r.PostID,
avg(r.FirstScore*iif(ct.id=1,p.COMCrtWeight/100,p.PROCrtWeight/100))
as WeightedSum from results r
inner join post p on r.PostID = p.ID
inner join job j on p.JobID = j.ID
inner join subcrt sc on r.SubCrtID = sc.ID
inner join Crt c on sc.CrtID = c.ID
inner join CrtType ct on c.CrtTypeID = ct.ID
where r.SeasonID = 4 and j.UnitID = 3068
group by ct.id, r.EvaluaTEDID, r.PostID)
group by EvaluaTEDID
买我的语法错误。
你能帮忙吗?提前致谢。