加入两个查询或联合/联合全部

时间:2014-09-05 15:47:47

标签: sql sql-server

我已经从下面的代码创建了一个联合。我不确定这是我想要的。顶部分为较小的细节。它按学校,年级,engProf和种族的考试成绩计算学生人数。第二个查询给出了按学生,学校,年级和考试分类的学生总数。回到学校x的二年级学生总数进行了数学测试,x学生参加了阅读测试。一个将是另一个的更高级别的摘要。我想将它们连接在一起,以便在报告中使用一个数据集。有什么建议。我试过一个工会,不确定这是不是最好的解决方案。


SELECT 
track,
schoolc,
schname AS[school],
grade,
subtestc AS[ELA/Math],
EngProf,
SUM(CASE WHEN (subtestc) IN ('ela','Math') AND tscrtypc ='A'THEN 1 ELSE 0 END) AS [total Students],
SUM(CASE WHEN (testscore)IN ('A','P') AND subtestc IN ('ela','Math') AND tscrtypc ='A'THEN 1 ELSE 0 END) AS [At/Above],
SUM(CASE WHEN (testscore)IN ('B','BB','FBB') AND subtestc IN ('ela','Math') AND tscrtypc ='A'THEN 1 ELSE 0 END) AS [Below],
SUM(CASE WHEN (subtestc) IN ('ela','Math') AND tscrtypc ='A' AND(ethnic)='White (not Hispanic)' THEN 1 ELSE 0 END) AS [Total White(not Hispanic)],
SUM(CASE WHEN (testscore)IN ('A','P') AND subtestc IN ('ela','Math') AND tscrtypc ='A' AND(ethnic)='White (not Hispanic)'THEN 1 ELSE 0 END) AS [White (Total not Hispanic) At/Above],
SUM(CASE WHEN (testscore)IN ('B','BB','FBB') AND subtestc IN ('ela','Math') AND tscrtypc ='A' AND(ethnic)='White (not Hispanic)'THEN 1 ELSE 0 END) AS [ Total White (not Hispanic)],
SUM(CASE WHEN (subtestc) IN ('ela','Math') AND tscrtypc ='A' AND(ethnic) <>'White (not Hispanic)' THEN 1 ELSE 0 END) AS [ Total Other Nonwhite students],
SUM(CASE WHEN (testscore)IN ('A','P') AND subtestc IN ('ela','Math') AND tscrtypc ='A' AND (ethnic)<>'White (not Hispanic)'THEN 1 ELSE 0 END) AS [TotalOther Non_White At/Above],
SUM(CASE WHEN (testscore)IN ('B','BB','FBB') AND subtestc IN ('ela','Math') AND tscrtypc ='A' AND (ethnic)<>'White (not Hispanic)'THEN 1 ELSE 0 END) AS [Total Other Non-White Below],
NULL AS [Grand total Students]

FROM [dbo].[qw_star_testing_detail]
WHERE subtestc IN('ela','Math')
AND tscrtypc ='A' 
AND testscore NOT IN('9')
GROUP BY track,
schoolc,
schname,
track,
grade,
EngProf,
subtestc


UNION ALL 

SELECT 
track,--NULL AS [track],schoolc,     
schoolc, ---NULL AS [schoolc],
schname AS[school],---NULL AS[school],
grade,---NULL AS [grade,],
subtestc AS[ELA/Math],--NULL AS[ELA/Math],
NULL AS[engProf],
null AS [total Students],
null AS [At/Above],
NULL AS [Below],
null AS [Total White(not Hispanic)],
NULL as [White (Total not Hispanic) At/Above],
null AS [ Total White (not Hispanic)],
NULL AS [ Total Other Nonwhite students],
null AS [TotalOther Non_White At/Above],
null AS [Total Other Non-White Below],
SUM(CASE WHEN (subtestc) IN ('ela','Math') AND tscrtypc ='A'THEN 1 ELSE 0 END) AS [Grand total Students]

FROM [dbo].[qw_star_testing_detail]
WHERE subtestc IN('ela','Math')
AND tscrtypc ='A' 
AND testscore NOT IN('9')
GROUP BY track,
schoolc,
schname,
track,
grade,
--EngProf,
subtestc
--ethnic


2 个答案:

答案 0 :(得分:1)

你不想只想离开外面加入2吗? 左边的桌子有更细的纹理(更详细)。

您可以使用CTE。

With
Group1
AS
(SELECT 
track,
schoolc,
schname AS[school],
grade,
subtestc AS[ELA/Math],
EngProf,
SUM(CASE WHEN (subtestc) IN ('ela','Math') AND tscrtypc ='A'THEN 1 ELSE 0 END) AS [total Students],
SUM(CASE WHEN (testscore)IN ('A','P') AND subtestc IN ('ela','Math') AND tscrtypc ='A'THEN 1 ELSE 0 END) AS [At/Above],
SUM(CASE WHEN (testscore)IN ('B','BB','FBB') AND subtestc IN ('ela','Math') AND tscrtypc ='A'THEN 1 ELSE 0 END) AS [Below],
SUM(CASE WHEN (subtestc) IN ('ela','Math') AND tscrtypc ='A' AND(ethnic)='White (not Hispanic)' THEN 1 ELSE 0 END) AS [Total White(not Hispanic)],
SUM(CASE WHEN (testscore)IN ('A','P') AND subtestc IN ('ela','Math') AND tscrtypc ='A' AND(ethnic)='White (not Hispanic)'THEN 1 ELSE 0 END) AS [White (Total not Hispanic) At/Above],
SUM(CASE WHEN (testscore)IN ('B','BB','FBB') AND subtestc IN ('ela','Math') AND tscrtypc ='A' AND(ethnic)='White (not Hispanic)'THEN 1 ELSE 0 END) AS [ Total White (not Hispanic)],
SUM(CASE WHEN (subtestc) IN ('ela','Math') AND tscrtypc ='A' AND(ethnic) <>'White (not Hispanic)' THEN 1 ELSE 0 END) AS [ Total Other Nonwhite students],
SUM(CASE WHEN (testscore)IN ('A','P') AND subtestc IN ('ela','Math') AND tscrtypc ='A' AND (ethnic)<>'White (not Hispanic)'THEN 1 ELSE 0 END) AS [TotalOther Non_White At/Above],
SUM(CASE WHEN (testscore)IN ('B','BB','FBB') AND subtestc IN ('ela','Math') AND tscrtypc ='A' AND (ethnic)<>'White (not Hispanic)'THEN 1 ELSE 0 END) AS [Total Other Non-White Below],
NULL AS [Grand total Students]

FROM [dbo].[qw_star_testing_detail]
WHERE subtestc IN('ela','Math')
AND tscrtypc ='A' 
AND testscore NOT IN('9')
GROUP BY track,
schoolc,
schname,
track,
grade,
EngProf,
subtestc)
,
Group2
AS
(SELECT 
track,--NULL AS [track],schoolc,     
schoolc, ---NULL AS [schoolc],
schname AS[school],---NULL AS[school],
grade,---NULL AS [grade,],
subtestc AS[ELA/Math],--NULL AS[ELA/Math],
NULL AS[engProf],
null AS [total Students],
null AS [At/Above],
NULL AS [Below],
null AS [Total White(not Hispanic)],
NULL as [White (Total not Hispanic) At/Above],
null AS [ Total White (not Hispanic)],
NULL AS [ Total Other Nonwhite students],
null AS [TotalOther Non_White At/Above],
null AS [Total Other Non-White Below],
SUM(CASE WHEN (subtestc) IN ('ela','Math') AND tscrtypc ='A'THEN 1 ELSE 0 END) AS [Grand total Students]

FROM [dbo].[qw_star_testing_detail]
WHERE subtestc IN('ela','Math')
AND tscrtypc ='A' 
AND testscore NOT IN('9')
GROUP BY track,
schoolc,
schname,
track,
grade,
--EngProf,
subtestc
--ethnic)

Select group1.track,   
group1.schoolc, group1.[school]
,group2. whatever other fields you want
From Group1
Left outer join Group2
 ON Group1.track=group2.track
 AND Group1.schoolc=group2.schoolc
 and group1.school = group2.school
...

答案 1 :(得分:0)

这是一种相当普遍的做法,有一个技巧...添加订单或级别列以正确的顺序获得结果。由于您无法正确排序,因此您对所分组的项目没有空值。这是一个例子,我列出了班级学生和总人数:

   SELECT class, student_name, count
   FROM
   (
     SELECT class, student_name, 0 as count, 1 as ord
     FROM classlist
     UNION ALL
     SELECT class, '' as student_name, count() as count, 2 as ord
     FROM classlist
     GROUP BY class
   ) T
   ORDER BY class, ord, student_name