加入两个具有分组的查询

时间:2016-01-18 16:03:10

标签: sql-server ms-access union

我试图计算一个评分,一个计数和一个基于将评级除以计数的指数。

但是我仍然坚持获得评级并计算查询以输出相同的结果。

我有以下两个问题:

SELECT
    DerivedStructuralRatingQuery.Pipe_Segment_Reference,
    SUM(DerivedStructuralRatingQuery.Structural_Rating) AS Structural_Score
FROM
    (
    SELECT Inspections.Pipe_Segment_Reference,
    (COUNT(*) * Conditions.structural_grade) AS Structural_Rating
    FROM (
        SELECT Inspections.Pipe_Segment_Reference, Conditions.structural_grade
        FROM Conditions
        INNER JOIN Inspections
        ON Conditions.InspectionID = Inspections.InspectionID
        WHERE Conditions.structural_grade IS NOT NULL
    )
    GROUP BY Inspections.Pipe_Segment_Reference, Conditions.structural_grade
    ) DerivedStructuralRatingQuery 
GROUP BY
    DerivedStructuralRatingQuery.Pipe_Segment_Reference;

-

SELECT 
    Inspections.Pipe_Segment_Reference,
    COUNT(*) AS Defects
FROM (
    SELECT Inspections.Pipe_Segment_Reference, Conditions.structural_grade
    FROM Conditions
    INNER JOIN Inspections
    ON Conditions.InspectionID = Inspections.InspectionID
    WHERE Conditions.structural_grade IS NOT NULL
    )
GROUP BY Inspections.Pipe_Segment_Reference, Conditions.structural_grade

期望的输出:

ID, Rating, Count
1, 5, 10
2, 3, 4

但是,我不知道如何将它们组合成单独的输出。我尝试将这两个查询作为一个查询但遇到了两个COUNT语句的问题。

我认为我需要的是UNION,但如果是这样的话,我无法弄清楚语法。

1 个答案:

答案 0 :(得分:1)

我认为您需要做的就是根据Pipe_Segment_Reference加入来自上述2个查询的结果,如下所示

SELECT A.Pipe_Segment_Reference
      ,A.Structural_Score 
      ,B.Defects 
      ,(A.Structural_Score / B.Defects) AS [Index]
  FROM 
       (
         SELECT
               DerivedStructuralRatingQuery.Pipe_Segment_Reference,
               SUM(DerivedStructuralRatingQuery.Structural_Rating) AS Structural_Score
           FROM
                (
                  SELECT Inspections.Pipe_Segment_Reference,
                         (COUNT(*) * Conditions.structural_grade) AS Structural_Rating
                   FROM (
                          SELECT Inspections.Pipe_Segment_Reference, Conditions.structural_grade
                            FROM Conditions
                           INNER JOIN Inspections
                              ON Conditions.InspectionID = Inspections.InspectionID
                           WHERE Conditions.structural_grade IS NOT NULL
                        )
                  GROUP BY Inspections.Pipe_Segment_Reference, Conditions.structural_grade
              ) DerivedStructuralRatingQuery 
         GROUP BY
                  DerivedStructuralRatingQuery.Pipe_Segment_Reference
       ) A
 INNER
  JOIN (
            SELECT 
                   Inspections.Pipe_Segment_Reference,
                   COUNT(*) AS Defects
             FROM (
                      SELECT Inspections.Pipe_Segment_Reference, Conditions.structural_grade
                        FROM Conditions
                       INNER JOIN Inspections
                          ON Conditions.InspectionID = Inspections.InspectionID
                       WHERE Conditions.structural_grade IS NOT NULL
                  )
             GROUP BY Inspections.Pipe_Segment_Reference, Conditions.structural_grade
       ) B
    ON A.Pipe_Segment_Reference = B.Pipe_Segment_Reference

希望这有帮助。

注意:以上内容仅以您的查询为基础,并不会尝试在内部对其进行优化以获得结果。