0

以下に 3 つのクエリがあります。それらを 1 つのクエリに結合して、郡ごとの結果を含む 3 つの列を取得したいと考えています。すべてのテーブルで内部結合を試みましたが、悪いデータが得られました。これら 3 つのクエリを組み合わせて郡別にグループ化するにはどうすればよいですか?

select co.Description
from Counties as co
group by co.Description


select [Total DLL Children] = SUM(cd.NumberOfLanguageSpeakers)
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 Counties as co on p.County_Id = co.Id
group by co.Description


select
  [Total Children] = (SUM(demo.NumberOfPreschoolers)
     + SUM(demo.NumberOfToddlers) + SUM(demo.NumberOfInfants))
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 Counties as co on p.County_Id = co.Id
group By co.Description
4

1 に答える 1

1

サブクエリを使用してそれを行うことができます

 select co.Description, demo.[Total Children], cd.[Total DLL Children]
 from Classrooms as c
 left outer join (select SUM(NumberOfPreschoolers) + SUM(NumberOfToddlers) + SUM(NumberOfInfants) as [Total Children], Classroom_id 
                    from ClassroomDemographics group by Classroom_id) as demo on demo.Classroom_id = c.Id
 left outer join (select SUM(NumberOfLanguageSpeakers) as [Total DLL Children], Classroom_id 
                    from ClassroomDLL group by Classroom_id) as cd 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 Counties as co on p.County_Id = co.Id
 group By co.Description
于 2013-03-27T17:28:44.660 に答える