在SQL Server中对包含计数和分组的列求和

时间:2012-11-15 17:46:52

标签: sql-server-2008 tsql

我不太确定如何解决这个问题,但基本上我的查询工作正常。我所做的是单独计算作为调查问题结果的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

0 个答案:

没有答案