我从下面的代码创建了一个联合。我不确定这是我想要的。最上面的部分分成较小的细节。它通过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
您是否将此查询的结果导入报告包进行格式化?如果是这样,那么你应该能够提取更详细的记录,并使用你的报告包添加分组和聚合值的细节 – 2014-09-05 15:51:07
我很困惑,你从哪里和哪里和group by子句看起来是一样的。我错过了什么吗?你为什么需要工会或加入?在我看来,你可以将第二个查询的唯一总和移动到第一个查询,替换它的空值。 – Andrew 2014-09-05 15:52:58