Why MySQL order by clause in window function creates cumulative sum? - mysql

select orderNumber
, priceEach
, sum(priceEach) over(partition by orderNumber order by priceEach) as `running total`
, sum(priceEach) over(partition by orderNumber) as `total`
from orderdetails;
From above query, why don't I get cumulative sum in total and cumulative some only when I add order by?
from https://www.mysqltutorial.org/mysql-window-functions/
order by clause is "The ORDER BY clause specifies how the rows are ordered within a partition" Even though rows are not ordered why does that stop us from getting cumulative sum?

Related

How to find Maximum of average in group by clause in SQL

Hi I have a query like this
select avg(TotalSale) as value from Sales group by SalesPerson;
which gives me the output as
value
50.0000
250.0000
62.5000
I want to just get the max out of the values. What should i do?
I tried
select max(avg(TotalSale)) as value from Sales group by SalesPerson; which is wrong.
select avg(TotalSale) as value from Sales group by SalesPerson order by value desc limit 1; which works partially.
What would be better approach to do this?
Wrap your query....
select max(avgvalue)
from
(
select avg(totalsale) as avgvalue
from sales
group by salesperson
)z
Use order by and fetch one row:
select avg(TotalSale)
from Sales
group by SalesPerson
order by avg(TotalSale) desc
limit 1;
You can also get the SalePerson involved with this as well.
I guess it can be done in a single query .Try this :
SELECT
Max(Totalsale) as Value
FROM
Sales
ORDER BY avg(Totalsale)
LIMIT 1;

How to sum when they have same color with sql?

I have this table
i want to ignore productNo and sum all product count accordingly.
select sum(count), max(productNo)
from Table
where date between 117 and 118
group by product
this one gives wrong result...
I want to have sum of counts for each Product-ProductNo combination
try like below
select product,productno,sum(count) as result
from table_name
where productno='X1'
group by product,productno
seems you need the firts rows order by result
select product,productno,sum(count) as result
from table
group by product,productno
order by result
limit 1
Since you haven't tagged any DBMS so, i would use row_number():
select t.*
from (select product, productno, sum(count) as cnt,
row_number() over (partition by product order by sum(count) desc) as seq
from table t
group by product, productno
) t
where seq = 1;
You can also use LIMIT clause (but not for each product) :
select product, productno, sum(count) as cnt
from table t
group by product, productno
order by cnt desc
limit 1;
Some other DBMS requires TOP clause instead of LIMIT clause so, you can change accordingly but the idea would be same.
select sum(count), max(productNo)
from Table
where date between 117 and 118
group by product, productNo
with this it works :)

Sum of trade by all the companies in MySQL [delete]

I want to find out the company with the maximum capital[price*quantity] (Including all entries for that company in the whole table). Any suggestions?
I've done this:
SELECT symbol, SUM(amount*price) AS total
FROM orders
GROUP BY symbol
If FETCH FIRST WITH TIES is supported:
SELECT symbol, SUM(amount*price) AS total
FROM orders
GROUP BY symbol
ORDER BY total DESC
FETCH FIRST 1 ROW WITH TIES
If not:
SELECT symbol, SUM(amount*price) AS total
FROM orders
GROUP BY symbol
HAVING SUM(amount*price) = (SELECT SUM(amount*price) AS total
FROM orders
GROUP BY symbol
ORDER BY total DESC
LIMIT 1)
Your query should look like this.
SELECT symbol,SUM(amount*price) AS total FROM orders GROUP BY symbol order by total desc limit 1;
I am adding limit 1 as you want to find out the company with maximum capital.

Select query inside a select query

I am running the following query on the table with multiple records having different quantity fields but the same id.
SELECT MIN( quantity )
FROM ( SELECT *
FROM `ready_for_delivery`
WHERE joborderid LIKE 00065
ORDER BY joborderid DESC ) a
GROUP BY quantity
It is returning all the values and not the minimum value. Any ideas why? Thanks in anticipation.
I think this is what you looking for:
SELECT MIN( quantity ) as 'Min' FROM ( SELECT * FROM `ready_for_delivery` WHERE joborderid LIKE 00065 ORDER BY joborderid DESC )a
If you are only after a single value, you should not be using a group by.
Definition for Group by:
A GROUP BY clause works on the rows returned by a query by summarizing identical rows into a single/distinct group and returns a single row with the summary for each group, by using appropriate Aggregate function in the
SELECT list, like
COUNT()
SUM()
MIN()
MAX()
AVG().

max average value in MySql

How to get the maximum from averages values in MySQL? The following query returns average values of amounts from table orders grouped by customers.
SELECT AVG(amount)
FROM orders
GROUP BY cust;
I want to receive a maximum value from average values using a single query with aggregate functions. Using ORDER BY ... DESC LIMIT 1 surely works, but what I am interested in is getting the maximum average value using aggregate functions solely. Is it possible at all? Thanks
select max(avg_value)
from
(
SELECT AVG(amount) avg_value FROM orders GROUP BY cust
) tmp
I would do this with order by and limit:
SELECT AVG(o.amount) as avg_value
FROM orders o
GROUP BY cust
ORDER BY avg_value DESC
LIMIT 1;
This allows you to get the cust for the maximum as well.