Home > Blockchain >  How to get total and avg in a row of mySQL query
How to get total and avg in a row of mySQL query

Time:07-08

I have fetching following data through query

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

enter image description here

How can I the following data through query. I am using mySQL 5.7 version

enter image description here

CodePudding user response:

You need to use aggregate window functions with no partition by clause. Check below for the code.

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]
  • Related