如何将三个sql选择组合成一个查询

时间:2013-03-27 16:13:08

标签: sql

我有以下三个查询,我想将它们组合成一个查询,所以我得到三列结果按县。我尝试与所有表进行内部联接,但是我得到的数据不好。如何将这三个查询组合在一起并按县分组?

     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

2 个答案:

答案 0 :(得分: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 

答案 1 :(得分:0)

一种方法是将现有的查询转换为这样的内联表 - 您不需要从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
相关问题