Group By仅适用于一列的行

时间:2013-11-25 22:26:30

标签: mysql sql

我想将Group By用于一列的行,让我解释一下。

我的查询:

   SELECT
   m.name AS brand,
   opv.name AS model,
   opv.product_condition AS condition,
   (AVG(opv.final_price + opv.overhead_cost)) AS cost,
   opv.product_color AS color,
   COUNT(m.name) as quantity
   FROM `order` o
   JOIN order_veri AS ov
     ON o.order_id = ov.order_fk
   JOIN order_prod_veri AS opv
     ON ov.order_fk = opv.order_id
   JOIN product AS p
     ON opv.product_id = p.product_id
   JOIN manufacturer AS m
     ON p.manufacturer_id = m.manufacturer_id
   GROUP  BY model,
             product_condition

这是我得到的数据: -

  Brand       |    Model           |    Cost    |    Condition    |  Color   |     Quantity    

  -------------------------------------------------------
  Apple       |    iPhone 5        |    $50.95  |     Used        |  Black   |     2
  Blackberry  |    Blackberry 9900 |    $22.98  |     Used        |  Black   |     2
  Samsung     |    Galaxy S        |    $16.92  |     Used        |  White   |     2
  HTC         |    Rhyme           |    $60.42  |     New         |  Red     |     2
  Google      |    Google Nexus    |    $72.24  |     New         |  Blue    |     2
  Motorola    |    Razr            |    $9.68   |     Used        |  Silver  |     2
  Apple       |    iPad Air        |    $2.74   |     New         |  Silver  |     1

这就是我想要获取数据的方式(或者我希望它如何显示): -

  Brand       |    Model           |    Cost    |    Condition    |  Color   |     Quantity    

  -------------------------------------------------------
  Apple       |    iPhone 5        |    $50.95  |     Used        |  Black   |     2
  Apple       |    iPhone 5        |    $50.95  |     Used        |  White   |     

  Blackberry  |    Blackberry 9900 |    $22.98  |     Used        |  Black   |     3
  Blackberry  |    Blackberry 9900 |    $22.98  |     Used        |  White   |     
  Blackberry  |    Blackberry 9900 |    $22.98  |     Used        |  Black   |     

  HTC         |    Rhyme           |    $60.42  |     New         |  Red     |     2
  HTC         |    Rhyme           |    $60.42  |     New         |  Black   |     

  Google      |    Google Nexus    |    $72.24  |     New         |  Blue    |     2
  Google      |    Google Nexus    |    $72.24  |     New         |  Black   |     

  Motorola    |    Razr            |    $9.68   |     Used        |  Silver  |     2
  Motorola    |    Razr            |    $9.68   |     Used        |  White   |     2

  Apple       |    iPad Air        |    $2.74   |     New         |  Silver  |     1

请注意价格如何保持不变,但现在取而代之的是其他行,具体取决于显示相同模型属性的数量。

我想在这里实现: -

现在在颜色列中,由于分组,我没有为每个模型获得不同的颜色变化。例如,第一行中 iPhone 5 的数量为5,但它显示的颜色仅为第5行中的第一行。

同样,第二行中的模型 Blackberry 9900 ,数量为2,但只显示其中1个的颜色,因为它们都是分组有两种不同的颜色(白色和黑色)。

所以我想问的是,是否可以使用GROUP BY对成本进行分组?

如果您想澄清某些内容或提供更好的解释,请告知我们。

3 个答案:

答案 0 :(得分:0)

对于GROUP BY中未包含的任何列,您需要将其置于聚合函数中。

在您的情况下,我认为您的意思是按品牌,型号和Product_Condition进行分组,平均成本并连接颜色。

SELECT
   m.name AS brand,
   opv.name AS model,
   opv.product_condition AS condition,
   (AVG(opv.final_price + opv.overhead_cost)) AS cost,
   CONCAT_WS(',', opv.product_color) AS color,
   COUNT(m.name) as quantity
   FROM `order` o
   JOIN order_veri AS ov
     ON o.order_id = ov.order_fk
   JOIN order_prod_veri AS opv
     ON ov.order_fk = opv.order_id
   JOIN product AS p
     ON opv.product_id = p.product_id
   JOIN manufacturer AS m
     ON p.manufacturer_id = m.manufacturer_id
   GROUP  BY brand, model,
             product_condition

答案 1 :(得分:0)

从您的问题中不清楚您要做什么。很明显,您正在使用令人困惑的非标准MySQL扩展GROUP BY。您应该阅读MySQL手册的这一部分。

http://dev.mysql.com/doc/refman/5.5/en/group-by-extensions.html

要记住SQL的一点是,任何问题的难点都在于精确指定所需的结果集。

您在评论中说过,您要显示广告资源的详细信息列表,即广告资源中每个不同项目的一行。但是在详细列表中,您希望显示该品牌,型号和条件库存中所有商品的平均成本。这在SQL中很棘手,因为你正在混合苹果(细节)和果园(平均值)。

要做到这一点,你需要一个子查询和一个连接。子查询计算出您的平均成本。就是这样:

SELECT  m.name AS brand,
      opv.name AS model,
      opv.product_condition AS product_condition,
      AVG(opv.final_price + opv.overhead_cost) AS cost
 FROM ... the tables in your original ...
 JOIN ...     query in your question.
GROUP BY brand, model, product_condition

然后,您需要将此子查询视为虚拟表,并将其连接到主查询,如此。

SELECT  m.name AS brand,
      opv.name AS model,
      opv.product_condition AS product_condition,
      avg.cost,
      opv.product_color AS color
 FROM ... the tables in your original ...
 JOIN ...    query in your question.
 JOIN ...       in your question
 JOIN (
      SELECT  m.name AS brand,
            opv.name AS model,
            opv.product_condition AS product_condition,
            (AVG(opv.final_price + opv.overhead_cost)) AS cost
       FROM ...  the tables in your original
       JOIN ...     query in your question.
      GROUP BY brand, model, product_condition
      ) AS avg ON (     m.name=avg.name 
                   AND opv.name = avg.model
                   AND opv.product_condition = avg.product_condition)

现在,这看起来像一个可怕的令人讨厌的查询。从某种意义上说,因为您的规范有时需要详细记录,有时需要平均值。但你不应该担心表现;这将是可以容忍的。请注意,子查询中有一个GROUP BY子句,因为它是计算平均值。但是主查询中没有这样的子句,因为它呈现了详细记录(单个项目)。

如果您只是在寻找汇总结果,那么您可以选择几个。如果您试图分别显示不同颜色和条件的项目的平均成本。在这种情况下,你想要

 SELECT  m.name AS brand,
         opv.name AS model,
         opv.product_condition AS product_condition,
         AVG(opv.final_price + opv.overhead_cost) AS cost,
         opv.product_color AS color,
    FROM ...
    JOIN ... 
   GROUP BY brand, model, product_condition, color

但也许您想要显示商品的平均价格,无论颜色如何。在这种情况下,你想要

 SELECT  m.name AS brand,
         opv.name AS model,
         opv.product_condition AS product_condition,
         AVG(opv.final_price + opv.overhead_cost) AS cost,
         GROUP_CONCAT (opv.product_color) AS color,
    FROM ...
    JOIN ... 
   GROUP BY brand, model, product_condition

您是否了解SELECT ... GROUP BYSELECTGROUP BY中的每个项目如何被提及,或者受到摘要功能(SUM,{{1}在GROUP_CONCAT?这是SELECT的标准方式。

答案 2 :(得分:0)

如果你想为每种颜色(以及你的其他组条件)添加一行但是想要忽略颜色的平均值,则必须分两步:获得平均值,然后列出每种颜色的平均值。您可以通过2个查询或将AVG作为子查询来完成。

我不建议使用子查询,因为效率非常低。对于任何大型数据集或大批量应用程序,它都会很慢。但是如果你愿意的话,就在这里。

SELECT
  m.name AS brand,
  opv.name AS model,
  opv.product_condition AS condition,
  (
    SELECT AVG(opv.final_price + opv.overhead_cost) 
    FROM order_prod_veri as opv1
    JOIN product AS p1 ON opv1.product_id = p1.product_id
    JOIN manufacturer AS m1 ON p1.manufacturer_id = m1.manufacturer_id
    WHERE opv1.model = opv.model
      AND opv1.product_condition = opv.product_condition
      AND m1.name = m.name
    GROUP BY m1.brand, opv1.model, opv1.product_condition
  ) as cost
  opv.product_color AS color,
  COUNT(m.name) as quantity
FROM `order` o
JOIN order_veri AS ov ON o.order_id = ov.order_fk
JOIN order_prod_veri AS opv ON ov.order_fk = opv.order_id
JOIN product AS p ON opv.product_id = p.product_id
JOIN manufacturer AS m ON p.manufacturer_id = m.manufacturer_id
GROUP  BY brand, model, product_condition, color