2014-09-05 3 views
0

J'ai créé une union tout à partir du code ci-dessous. Je ne suis pas sûr que ce soit ce que je veux. La partie supérieure dans est le groupe dans les plus petits détails. Il compte le nombre d'élèves, par l'école, la note, les résultats des tests par engProf et ethniques. la deuxième requête donne un total d'étudiants ventilés par étudiant, école, niveau et test. Le retour du nombre total d'élèves de deuxième année à l'école x a été soumis au test de mathématiques et x élèves ont passé le test de lecture. L'un sera un résumé de niveau supérieur de l'autre. Je veux les joindre ensemble pour qu'un jeu de données soit utilisé dans un rapport. Aucune suggestion. J'ai essayé un syndicat, je ne sais pas si c'est la meilleure solution.Joindre deux requêtes ou une union/union tous


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

Souhaitez-vous extraire les résultats de cette requête dans un package de création de rapports? Si oui, alors vous devriez être capable d'extraire dans les dossiers plus détaillés et utiliser votre paquetage de rapport pour ajouter des groupements et agréger des valeurs pour les détails –

+0

Je suis confus, vos clauses from et where et group by se ressemblent à moi. Est-ce que je manque quelque chose? Pourquoi avez-vous besoin d'un syndicat ou d'une joint-venture? Il me semble que vous pourriez simplement déplacer votre seule somme de la deuxième requête à la première, en remplaçant la version nulle de celle-ci. – Andrew

Répondre

0

Ceci est une pratique assez courante avec un tour ... Ajouter dans une colonne d'ordre ou niveau pour obtenir les résultats dans le bon ordre. N'avez pas de valeur Null pour les éléments que vous regroupez car vous ne pouvez pas le commander correctement. Voici un exemple où je fais une liste d'un étudiants de classe et le nombre total:

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

Ne voulez-vous pas juste à gauche rejoindre le 2? Où la table de gauche a le grain le plus fin (plus de détails).

Vous pouvez utiliser un 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 
...