2013-03-27 111 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