我在下面有两个以下的查询,Total回来了不同,但我在每个查询中以相同的方式添加总和。为什么总回来会有所不同?
select [Total Children] = (SUM(demo.NumberOfPreschoolers) + SUM(demo.NumberOfToddlers) + SUM(demo.NumberOfInfants)),
County = co.Description
from ClassroomDemographics as demo
inner join Classrooms as c on demo.Classroom_Id = c.Id
inner join Sites as s on c.Site_Id = s.Id
inner join Profiles as p on s.Profile_Id = p.Id
inner join Dictionary.Counties as co on p.County_Id = co.Id
where co.Description = 'MyCounty'
Group By co.Description
select [Number Of DLL Children] = SUM(cd.NumberOfLanguageSpeakers),
[Total Children] = (SUM(demo.NumberOfPreschoolers) + SUM(demo.NumberOfToddlers) + SUM(demo.NumberOfInfants)),
County = co.Description
from ClassroomDLL as cd
inner join Classrooms as c on cd.Classroom_Id = c.Id
inner join Sites as s on c.Site_Id = s.Id
inner join Profiles as p on s.Profile_Id = p.Id
inner join Dictionary.Counties as co on p.County_Id = co.Id
inner join ClassroomDemographics as demo on c.Id = demo.Classroom_Id
where co.Description = 'MyCounty'
Group by co.Description
答案 0 :(得分:0)
快速浏览一下这两个阙,我猜想:
inner join ClassroomDemographics as demo on c.Id = demo.Classroom_Id
第二个查询中的排除了第一个查询中的结果,因此聚合值将不同。
答案 1 :(得分:0)
您对Classrooms
表的加入是在第二个查询中加入一个额外的表。
查询1:
from ClassroomDemographics as demo
inner join Classrooms as c on demo.Classroom_Id = c.Id
查询2:
from ClassroomDLL as cd
inner join Classrooms as c on cd.Classroom_Id = c.Id
...
inner join ClassroomDemographics as demo on c.Id = demo.Classroom_Id
我敢打赌,ClassroomDLL
表中的数据较少,或者其中一个连接条件列的行为空,其中任何一行都可以从结果中排除行并关闭总计。