Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/ruby-on-rails-4/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Mysql 获取连接三个表的聚合平均值,并将它们显示在第一个表中每个值的旁边_Mysql_Sql_Group By_Sum - Fatal编程技术网

Mysql 获取连接三个表的聚合平均值,并将它们显示在第一个表中每个值的旁边

Mysql 获取连接三个表的聚合平均值,并将它们显示在第一个表中每个值的旁边,mysql,sql,group-by,sum,Mysql,Sql,Group By,Sum,我有三个表格,您也可以在以下表格中找到: 第一个表包含每种产品的销售额 第二个表包含每个产品的详细信息 第三个表包含类别 现在,我想在每个产品旁边显示所有产品和每个类别的平均销售额价格 结果应该如下所示: Product_ID Category average_sales_price_per_category P001 Fashion 3.66 P002 Fashion

我有三个表格,您也可以在以下表格中找到:

第一个表包含每种产品的
销售额

第二个表包含每个
产品的详细信息
第三个表包含
类别


现在,我想在每个产品旁边显示所有产品和每个类别的平均销售额价格
结果应该如下所示:

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
我试图使用来自的解决方案,但出现了
错误

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'

我需要在代码中更改什么才能获得预期的结果?

您的表在内部子查询中不可见,以避免内部子查询中的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;

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

如果您运行的是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

| Product_ID | Category_Name | average_sales_price | | ---------- | ------------- | ------------------- | | P001 | Fashion | 3.6666666666666665 | | P002 | Fashion | 3.6666666666666665 | | P003 | Sport | 1.6 | | P004 | Sport | 1.6 | | P005 | Sport | 1.6 | | P006 | Shoes | 1.2857142857142858 | | P007 | Shoes | 1.2857142857142858 | |产品|标识|类别|名称|平均|销售|价格| | ---------- | ------------- | ------------------- | |P001 |时尚| 3.6665| |P002 |时尚| 3.6665| |P003 |体育| 1.6| |P004 |体育| 1.6| |P005 |体育| 1.6| |P006 |鞋| 1.2857142857142858| |P007 |鞋| 1.2857142857142858|
首先,在
Sales
表中没有任何
Category\u Id

根据不匹配案例的概率,将您的
JOIN
子句替换为
LEFT JOIN
s,并在末尾添加
GROUP BY c.Category\u Name,c.Category\u 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;
其中不需要相关子查询


谢谢你的回答,但我仍然有一个错误:我不得不稍微修改你的回答,但现在它给出了我需要的结果:@Michi谢谢,我很忙,没有时间给你更新的版本..谢谢你的回答,但这个查询显示的是每种产品而不是每种类别的平均销售价格:不客气,对吧。我现在已经重新整理了类别@Michi。
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;