【问题标题】:Get aggregated average values joining three tables and display them next to each value in first table获取连接三个表的聚合平均值并将它们显示在第一个表中的每个值旁边
【发布时间】:2020-02-28 09:28:11
【问题描述】:

我有三个表格,您也可以在SQL fiddle 中找到它们:

CREATE TABLE Sales (
    Product_ID VARCHAR(255),
    Sales_Value VARCHAR(255),
    Sales_Quantity VARCHAR(255)
);
INSERT INTO Sales
(Product_ID, Sales_Value, Sales_Quantity)
VALUES 
("P001", "500", "200"),
("P002", "600", "100"),
("P003", "300", "250"),
("P004", "900", "400"),
("P005", "800", "600"),
("P006", "200", "150"),
("P007", "700", "550");


CREATE TABLE Products (
    Product_ID VARCHAR(255),
    Product_Name VARCHAR(255),
    Category_ID VARCHAR(255)
);
INSERT INTO Products
(Product_ID, Product_Name, Category_ID)
VALUES 
("P001", "Shirt", "C001"),
("P002", "Dress", "C001"),
("P003", "Hoodie", "C002"),
("P004", "Ball", "C002"),
("P005", "Ski", "C002"),
("P006", "Boot", "C003"),
("P007", "Flip-Flop", "C003");


CREATE TABLE Categories (
    Category_ID VARCHAR(255),
    Category_Name VARCHAR(255)
);
INSERT INTO Categories
(Category_ID, Category_Name)
VALUES 
("C001", "Fashion"),
("C002", "Sport"),
("C003", "Shoes");

第一个表包含每个产品的Sales
第二个表包含有关每个product 的详细信息。
第三个表包含categories


现在,我想显示所有产品以及每个产品旁边的 average_sales_price_per_category
结果应如下所示:

Product_ID      Category      average_sales_price_per_category
P001             Fashion               3.66
P002             Fashion               3.66
P003             Sport                 1.60
P004             Sport                 1.60
P005             Sport                 1.60
P006             Shoes                 1.28
P007             Shoes                 1.28

我尝试使用this question 的解决方案,但我得到了Error

SELECT s.Product_ID, c.Category_Name,
       (SELECT SUM(SS.Sales_Value) / SUM(SS.Sales_Quantity)
        FROM Sales SS 
        WHERE SS.Category_ID = S.Category_ID
       ) AS average_sales_price
FROM Sales s 
JOIN Products p ON p.Product_ID = s.Product_ID
JOIN Categories c ON c.Category_ID = p.Category_ID;

错误

Unknown column 'SS.Category_ID' in 'where clause'

我需要对我的代码进行哪些更改才能获得预期的结果?

【问题讨论】:

    标签: mysql sql group-by sum


    【解决方案1】:

    您的查询的问题是类别在销售级别不可用。

    如果你运行的是 MySQL 8.0,你可以结合聚合和窗口函数如下:

    SELECT 
        p.Product_ID, 
        c.Category_Name,
        SUM(SUM(s.Sales_Value)) OVER(PARTITION BY c.Category_ID)
            / SUM(SUM(s.Sales_Quantity)) OVER(PARTITION BY c.Category_ID)
           AS average_sales_price
    FROM Sales s 
    JOIN Products p ON p.Product_ID = s.Product_ID
    JOIN Categories c ON c.Category_ID = p.Category_ID
    GROUP BY p.Product_ID, c.Category_ID, c.Category_Name
    

    Demo on DB Fiddle

    |产品ID |类别名称 | average_sales_price | | ---------- | ------------- | ------------------- | | P001 |时尚 | 3.6666666666666665 | | P002 |时尚 | 3.6666666666666665 | | P003 |运动 | 1.6 | | P004 |运动 | 1.6 | | P005 |运动 | 1.6 | | P006 |鞋 | 1.2857142857142858 | | P007 |鞋 | 1.2857142857142858 |

    【讨论】:

      【解决方案2】:

      为了避免内部子查询中的 where 条件,您的表在内部子查询中不可见,您可以在分组聚合子查询上使用联接

      SELECT
      s.Product_ID,
      Price_Category.average_sales_price_per_category
      FROM Sales s
      JOIN Products p ON p.Product_ID = s.Product_ID
      JOIN
        (SELECT 
        c.Category_ID,
        c.Category_Name,
        SUM(s.Sales_Value) / SUM(s.Sales_Quantity) AS average_sales_price_per_category
        FROM Sales s 
        JOIN Products p ON p.Product_ID = s.Product_ID
        JOIN Categories c ON c.Category_ID = p.Category_ID
        GROUP BY 1) Price_Category ON Price_Category.Category_ID = p.Category_ID;
      

      SQL Fiddle

      【讨论】:

      【解决方案3】:

      首先,Sales 表中没有任何 Category_Id 列。

      将您的JOIN 子句替换为LEFT JOINs,以防止出现不匹配的情况,并在末尾添加GROUP BY c.Category_Name, c.Category_ID

      SELECT c.Category_Name,c.Category_ID,
             SUM(S.Sales_Value) / SUM(S.Sales_Quantity) AS average_sales_price       
        FROM Sales s 
        LEFT JOIN Products p ON p.Product_ID = s.Product_ID
        LEFT JOIN Categories c ON c.Category_ID = p.Category_ID
       GROUP BY c.Category_Name, c.Category_ID;
      

      不需要相关子查询。

      Demo

      【讨论】:

      • 感谢您的回答,但此查询显示每个产品而非每个类别的平均 slaes 价格:sqlfiddle.com/#!9/8ccb55/12
      • 不客气,对。我现在已经重新排列了类别@Michi。
      猜你喜欢
      • 2019-08-09
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-08-05
      • 1970-01-01
      • 2018-08-24
      • 2015-07-11
      • 2013-11-28
      相关资源
      最近更新 更多