0

我在下面有以下两个查询,总计返回不同,但我以相同的方式在每个查询中添加总和。为什么返回的总数不同?

    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
4

2 回答 2

0

您对Classrooms表的联接正在与第二个查询中的一个额外表联接。

查询一:

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表中的数据较少,或者其中一个连接条件列的行为空,其中任何一个都可能从结果中排除行并抛出您的汇总总计。

于 2013-03-27T15:44:12.427 回答
0

快速浏览一下这两个问题,我认为:

inner join ClassroomDemographics as demo on c.Id = demo.Classroom_Id

在第二个查询中排除了第一个查询中的结果,因此聚合值会有所不同。

于 2013-03-27T15:32:52.407 回答