对单个查询进行分组,排序和计数

时间:2015-04-16 16:25:51

标签: mysql sql sorting count group-by

我在单个查询中尝试GROUPSORTCOUNT,其中一个名为' goods'。

以下是我的MySql表格的简化:

family  sub_family  name        detailed_name 
Agro    Grains      Wheat       Wheat per 1 mt
Agro    Grains      Corn        Corn per 1 mt
Agro    Grains      Sugar       Sugar per 1 mt
Agro    Fruits      Apple       Apple red
Agro    Fruits      Apple       Apple green
Agro    Fruits      Apple       Apple yellow
Agro    Fruits      Lemon       Lemon classic
Wood    Tree        Lemon       Lemon in logs
Wood    Tree        Oak         Oak in logs
Wood    Tree        Epicea      Epicea in logs
Wood    Packaging   Kraftliner  Krafliner 3mm

我想:

  1. GROUP name
  2. {li> SORT familysub_family,最后name
  3. COUNT每个familysub_familyname的行数(同上sub_family
  4. 到目前为止,我设法在同一个COUNT中完成除sub_family之外的所有操作。

    确实,以下查询:

    SELECT 
        TableC.family,
        TableC.NbrFamily,
        TableB.sub_family,
        TableB.NbrSubFamily,
        TableA.name,
        TableA.NbrName
    FROM 
    (
        SELECT 
            family,
            sub_family,
            name,
            COUNT(DISTINCT commodities.id) AS NbrName 
        FROM commodities GROUP BY name
    ) TableA
    INNER JOIN 
    (
        SELECT 
            sub_family,
            COUNT(DISTINCT commodities.id) AS NbrSubFamily 
        FROM commodities GROUP BY sub_family
    ) TableB
    ON (TableA.sub_family = TableB.sub_family)
    INNER JOIN 
    (
        SELECT 
            family,
            COUNT(DISTINCT commodities.id) AS NbrFamily 
        FROM commodities GROUP BY family
    ) TableC
    ON (TableA.family = TableC.family)
    GROUP BY TableA.name
    ORDER BY TableA.family,TableA.sub_family,TableA.name
    

    导致以下结果:

    family  NbrFamily  sub_family  NbrSubFamily  name        NbrName
    Agro    7          Grains      3             Wheat       1       
    Agro    7          Grains      3             Corn        1        
    Agro    7          Grains      3             Sugar       1       
    Agro    7          Fruits      4             Apple       3
    Agro    7          Fruits      4             Lemon       2
    Wood    4          Tree        3             Lemon       2
    Wood    4          Tree        3             Oak         1     
    Wood    4          Tree        3             Epicea      1  
    Wood    4          Packaging   1             Kraftliner  1
    

    您可以看到NbrName计算柠檬 2次,但我希望它只计算一次,因为 lemon Fruits中 sub_family和另一个 sub_family

    [更新]:这是我想要的结果:

       family  NbrFamily  sub_family  NbrSubFamily  name        NbrName
        Agro    7          Grains      3             Wheat       1       
        Agro    7          Grains      3             Corn        1        
        Agro    7          Grains      3             Sugar       1       
        Agro    7          Fruits      4             Apple       3
        Agro    7          Fruits      4             Lemon       1
        Wood    4          Tree        3             Lemon       1
        Wood    4          Tree        3             Oak         1     
        Wood    4          Tree        3             Epicea      1  
        Wood    4          Packaging   1             Kraftliner  1
    

1 个答案:

答案 0 :(得分:2)

我想你要问http://sqlfiddle.com/#!9/e9206/16

因为它带来了预期的结果:

 SELECT A.family, C.NbrFamily,A.sub_family,B.NbrSubFamily,A.name,COUNT(A.Name)
 FROM  commodities as A
LEFT JOIN (
  SELECT family,sub_family,COUNT(Name) AS NbrSubFamily 
  FROM commodities 
  GROUP BY family,sub_family
) B
ON A.sub_family = B.sub_family 
  AND A.family = B.family 
LEFT JOIN (
  SELECT family,COUNT(Name) AS NbrFamily 
  FROM commodities 
  GROUP BY family
) C

ON  A.family = C.family 
GROUP BY A.family,A.sub_family,A.name
ORDER BY A.id