2014-09-05 73 views
0

我从下面的代码创建了一个联合。我不确定这是我想要的。最上面的部分分成较小的细节。它通过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 


+1

您是否将此查询的结果导入报告包进行格式化?如果是这样,那么你应该能够提取更详细的记录,并使用你的报告包添加分组和聚合值的细节 – 2014-09-05 15:51:07

+0

我很困惑,你从哪里和哪里和group by子句看起来是一样的。我错过了什么吗?你为什么需要工会或加入?在我看来,你可以将第二个查询的唯一总和移动到第一个查询,替换它的空值。 – Andrew 2014-09-05 15:52:58

回答

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 
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 
...