我对此查询有疑问:
select
distinct count(avancementtotal1.id_Module) as 'number module not realised'
,count(avancementtotal2.id_module) as 'number module realised'
,Groupe.name_Groupe,Groupe.Year
From
AvancementTotal avancementtotal1
join
Module on Module.id_Module = avancementtotal1.id_Module
join
Affilate on Module.id_Affilate = Affilate.id_Affilate
join
Groupe on Groupe.id_Groupe = avancementtotal1.id_Groupe
join
AvancementTotal avancementtotal2 on avancementtotal1.id = avancementtotal2.id
where
avancementtotal2.State_efm = 'EFM Realised'
and avancementtotal1.State_efm = 'EFM Not REALISEd'
and avancementtotal1.id <> avancementtotal2.id
Group by
Groupe.id_Groupe, Groupe.name_Groupe, Groupe.Year, Affilate.name_Affilate
它返回null,我想知道原因。
我希望模块的数量没有实现,并且在同一时刻有了实现EFM状态的模块
这里的结构:
答案 0 :(得分:0)
我认为您希望将此作为条件聚合:
select sum(case when avt.State_efm = 'EFM Not REALISEd' then 1 else 0 end) as NumNotRealized,
sum(case when avt.State_efm = 'EFM Realised' then 1 else 0 end) as NumRealized,
g.name_Groupe, g."Year"
From AvancementTotal avt join
Module m
on m.id_Module = avt.id_Module join
Affilate a
on m.id_Affilate = a.id_Affilate join
Groupe g
on g.id_Groupe = avt.id_Groupe
Group by g.id_Groupe, g.name_Groupe, g."Year", a.name_Affilate;
请注意,这只会加入AvancementTotal
表一次,并使用条件select
替换sum()
中的计数。
我还做了以下更改:
year
附近添加双引号,因为它是保留字我还怀疑a.name_Affilate
应该在select
子句中,或者不在group by
子句中。