【问题标题】:How to get total and avg in a row of mySQL query如何在一行 mySQL 查询中获取总计和平均值
【发布时间】:2022-07-07 13:18:19
【问题描述】:

我通过查询获取了以下数据

select ord_date,sum(product_sales) product_sales from salestable group by ord_date

如何通过查询获得以下数据。我使用的是 mySQL 5.7 版本

【问题讨论】:

    标签: mysql


    【解决方案1】:

    您需要使用不带 partition by 子句的聚合窗口函数。检查下面的代码。

    SELECT 
        ord_date, 
        product_sales,
        SUM(product_sales) OVER() as total_sales,
        ROUND(AVG(product_sales) OVER(),2) as avg_sales
    FROM Orders;
    
    Result_Set:
    
    [Result_01][1]
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2010-12-07
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-08-17
      • 1970-01-01
      • 2018-07-25
      • 1970-01-01
      相关资源
      最近更新 更多