0

我不太确定如何解决这个问题,但基本上我的查询工作正常。我所做的是单独计算 6 列作为调查问题的结果。然后我把它们加起来。现在我必须做 3 组计数,因为每个结果都有效,红色、琥珀色和绿色,并且用户只想要一行。也就是红色、琥珀色和绿色的总数。但是,用户现在希望获得返回相同信息但针对每个站点的结果。

那么,有没有一种简单的方法可以让我获得每个列或问卷类别的计数,将它们相加,以便我得到红色、琥珀色和绿色的总数,然后按每个站点的这个分组?SQL 在下面,但它适用于一个站点。

   Select production_site, SUM(Green.[Bagging]+Green.[Measurable]+
   Green.[Appearance]+Green.[Aroma]+Green.[Flavour]+Green.[Texture]) as Target,
   SUM(Amber.[Bagging]+Amber.[Measurable]+Amber.[Appearance]+Amber.[Aroma]+
   Amber.[Flavour]+Amber.[Texture]) as Action, SUM(Red.[Bagging]+Red.[Measurable]+
   Red.[Appearance]+Red.[Aroma]+Red.[Flavour]+Red.[Texture]) as Fail
   FROM (SELECT (SELECT COUNT(Grocery_Packaging_And_Coding)
             FROM t_Pqe_Grocery
             WHERE Grocery_Packaging_And_Coding = 'Target'
             AND Production_Site = 'Bradford'
             ) As Bagging,
             (SELECT COUNT(Grocery_Measurable)
              FROM t_Pqe_Grocery
              WHERE Grocery_Measurable = 'Target'
              AND Production_Site = 'Bradford'
              ) As Measurable,
             (SELECT COUNT(Grocery_Appearance)
              FROM t_Pqe_Grocery
              WHERE Grocery_Appearance = 'Target'
              AND Production_Site = 'Bradford'
              ) As Appearance,
             (SELECT COUNT(Grocery_Aroma)
              FROM t_Pqe_Grocery
              WHERE Grocery_Aroma = 'Target'
              AND Production_Site = 'Bradford'
              ) As Aroma,
             (SELECT COUNT(Grocery_Flavour)
              FROM t_Pqe_Grocery
              WHERE Grocery_Flavour = 'Target'
             AND Production_Site = 'Bradford'
             ) As Flavour,
            (SELECT COUNT(Grocery_Texture)
             FROM t_Pqe_Grocery
             WHERE Grocery_Texture = 'Target'
            AND Production_Site = 'Bradford'
            ) As Texture) AS Green,
    (SELECT (SELECT COUNT(Grocery_Packaging_And_Coding)
             FROM t_Pqe_Grocery
             WHERE Grocery_Packaging_And_Coding = 'Action'
             AND Production_Site = 'Bradford'
             ) As Bagging,
            (SELECT COUNT(Grocery_Measurable)
             FROM t_Pqe_Grocery
             WHERE Grocery_Measurable = 'Action'
             AND Production_Site = 'Bradford'
             ) As Measurable,
            (SELECT COUNT(Grocery_Appearance)
             FROM t_Pqe_Grocery
             WHERE Grocery_Appearance = 'Action'
             AND Production_Site = 'Bradford'
            ) As Appearance,
           (SELECT COUNT(Grocery_Aroma)
            FROM t_Pqe_Grocery
            WHERE Grocery_Aroma = 'Action'
            AND Production_Site = 'Bradford'
            ) As Aroma,
           (SELECT COUNT(Grocery_Flavour)
            FROM t_Pqe_Grocery
            WHERE Grocery_Flavour = 'Action'
            AND Production_Site = 'Bradford'
           ) As Flavour,
           (SELECT COUNT(Grocery_Texture)
            FROM t_Pqe_Grocery
            WHERE Grocery_Texture = 'Action'
            AND Production_Site = 'Bradford'
           ) As Texture) AS Amber,
    (SELECT (SELECT COUNT(Grocery_Packaging_And_Coding)
           FROM t_Pqe_Grocery
           WHERE Grocery_Packaging_And_Coding = 'Fail'
           AND Production_Site = 'Bradford'
          ) As Bagging,
          (SELECT COUNT(Grocery_Measurable)
           FROM t_Pqe_Grocery
           WHERE Grocery_Measurable = 'Fail'
           AND Production_Site = 'Bradford'
          ) As Measurable,
          (SELECT COUNT(Grocery_Appearance)
           FROM t_Pqe_Grocery
           WHERE Grocery_Appearance = 'Fail'
           AND Production_Site = 'Bradford'
          ) As Appearance,
          (SELECT COUNT(Grocery_Aroma)
           FROM t_Pqe_Grocery
           WHERE Grocery_Aroma = 'Fail'
           AND Production_Site = 'Bradford'
          ) As Aroma,
          (SELECT COUNT(Grocery_Flavour)
           FROM t_Pqe_Grocery
           WHERE Grocery_Flavour = 'Fail'
           AND Production_Site = 'Bradford'
          ) As Flavour,
          (SELECT COUNT(Grocery_Texture)
           FROM t_Pqe_Grocery
           WHERE Grocery_Texture = 'Fail'
           AND Production_Site = 'Bradford'
          ) As Texture) AS Red,
    t_Pqe_Grocery
    Where Production_Site = 'Bradford'
    Group By production_site
4

0 回答 0