加入两个具有分组依据的查询

Joining two queries that have group by's

我正在尝试计算评级、计数和基于评级除以计数的指数。

但是我一直坚持让评级和计数查询输出相同的结果。

我有以下两个问题:

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,但如果是这种情况,我无法弄清楚语法。

我认为您需要做的就是根据 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

希望对您有所帮助。

注意:以上只是建立在您的查询之上,并不试图在内部优化它们以获得结果。