2013-03-27 77 views
-1

我有三个查询在下面,我想将它们合并成一个查询,所以我得到三个县的结果列。我尝试了与所有表的内部连接,但是我收到了错误的数据。我如何结合这三个查询和县组?如何将三个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 
+0

我们需要比您提供的更多信息 – Woot4Moo 2013-03-27 16:14:03

+0

您需要哪些信息? – user1220099 2013-03-27 16:14:31

+0

输出显示副结果。解释计划。一个SQLFiddle – Woot4Moo 2013-03-27 16:17:22

回答

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 
+0

正是我所期待的。非常感谢,这是一个巨大的帮助.... – user1220099 2013-03-27 16:56:46

+0

不客气。乐意效劳。 – ljh 2013-03-27 17:06:34

+0

我该如何改变它只带回列有COUNT(c。[总童年]> 0) – user1220099 2013-03-27 17:11:53

0

一种方式做这将是现有的查询转换为内联表这样的 - 你不需要从县表中选择,你已经得到了表中的前两个疑问:

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