-1

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

     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

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

2 に答える 2

1

これを試してください。基本的に、各サブクエリは、County.Description も返す必要があります。その後、それらを結合できます。


 SELECT A.Description, B.[Total DLL Children], C.[Total Children]
 FROM (
     select co.Description from Counties as co
     group by co.Description
     ) A
 LEFT JOIN 
     (
         select co.Description, [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
      ) B
      ON A.DESCRIPTION = B.DESCRIPTION 
LEFT JOIN 
      (
         select co.Description, [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
      ) C
      ON A.DESCRIPTION = C.DESCRIPTION 
于 2013-03-27T16:36:36.137 に答える
0

これを行う 1 つの方法は、既存のクエリを次のようなインライン テーブルに変換することです。最初の 2 つのクエリで既にそのテーブルを取得しているため、Counties テーブルから選択する必要はありません。

select co.Description, a.[Total DLL Children], b.[Total Children]
from
(select co.id,[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.id, co.Description) a
 join 
 (select co.id, co.Description, [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.id, co.Description) b on a.id = b.id
于 2013-03-27T16:38:13.940 に答える