将两个查询与不同的“ FROM”表但相似的“ JOIN”表组合

时间:2019-03-15 17:04:53

标签: sql-server

我有两个要组合成一个结果集的查询。

查询1:

SELECT t1.evalID, t2.[Order], COUNT(t2.StepID) AS 'Total Categories'
FROM Evals t1
JOIN Steps t2 ON t1.TemplateID = t2.TemplateID 
JOIN GradingCats t3 ON t2.StepID = t3.StepID 
GROUP BY t1.EvalID, t2.[Order]
ORDER BY t2.[Order]

查询2:

SELECT t4.EvaluatorID, t6.StepID, t6.[Order], COUNT(t4.Grade) AS 'Grades Entered'
FROM Grading t4
JOIN GradingCats t5 ON t4.GradingCatID = t5.GradingCatID 
JOIN Steps t6 ON t5.StepID = t6.StepID
GROUP BY t6.StepID, t4.EvaluatorID, t6.[Order]

我的最终目标是找出评估中哪些步骤缺少分数。

编辑(样本数据): 查询#1

|---------------------|------------------|---------------------|
|      evalID         |     Order        |  Total Categories   |
|---------------------|------------------|---------------------|
|          81         |      01.00       |         17          |
|---------------------|------------------|---------------------|
|          81         |      02.00       |         17          |
|---------------------|------------------|---------------------|
|          81         |      03.00       |         17          |
|---------------------|------------------|---------------------|

查询#2

|---------------------|------------------|---------------------|------------------|
|     evaluatorID     |       Step       |        Order        |   Grades Entered |
|---------------------|------------------|---------------------|------------------|
|        1178         |        609       |        01.00        |        2         |
|---------------------|------------------|---------------------|------------------|
|        1178         |        615       |        02.00        |        3         |
|---------------------|------------------|---------------------|------------------|
|        9441         |        609       |        01.00        |        17        |
|---------------------|------------------|---------------------|------------------|
|        9441         |        609       |        02.00        |        17        |
|---------------------|------------------|---------------------|------------------|
|        9441         |        609       |        03.00        |        17        |
|---------------------|------------------|---------------------|------------------|

2 个答案:

答案 0 :(得分:0)

从显示与EVAL相关的所有步骤的第一个查询开始,您可以在第二个查询中左移JOIN,而在查询右侧为NULL的步骤将是那些缺少成绩的步骤。 / p>

为此,表中必须有某种方式将Grading链接到Evals。从您发布的代码中看不出此列,但我认为它在那里。也许是通过GradingCats

在缩短的伪代码中,只是为了表明我的意思:

SELECT ...
FROM Evals e
INNER JOIN Steps s ON e.TemplateID = s.TemplateID 
LEFT OUTER JOIN Grading g ON g.EvalID = e.EvalID  --use whatever means you have to show which Eval a Grade is from
LEFT OUTER JOIN Steps gs ON {join to Grading through GradingCats as in your second query}
WHERE gs.StepID IS NULL

在分析此查询的结果时,每个评估的所有步骤都将在s.StepID中,并且当同一行的gs.StepID为NULL时,这意味着该步骤未获得评分

请注意,由于要进行行级分析,因此您不想在此查询中执行任何GROUP BY。

答案 1 :(得分:0)

一个同事(比我更了解数据)对我的查询做了些修改:

SELECT query1.stepID, Categories, Graded
FROM
(
    SELECT rs.stepid, COUNT(c.category) AS 'Categories'
    FROM Evals e
    JOIN RunScriptSteps rs ON e.TemplateID = rs.TemplateID
    JOIN GradingCats c ON rs.StepID = c.StepID
    WHERE EvalID = *(someNumber)*
    GROUP BY rs.stepid
)AS query1
LEFT JOIN
(
    SELECT s.StepID, COUNT(Grade) AS 'Graded'
    FROM Grading g
    JOIN GradingCats c ON g.GradingCatID = c.GradingCatID
    JOIN Steps s ON c.StepID = s.StepID
    WHERE EvalID = *(someNumber)*
    GROUP BY s.stepid
) AS query2
ON query1.stepid = query2.stepid
ORDER BY stepid ASC