how to calculate aggregate function using mysql database.The following query returns invalid use of group functions.
select avg(max(qty)) from tb_name.
how to calculate aggregate function using mysql database.The following query returns invalid use of group functions.
select avg(max(qty)) from tb_name.
You do not have any groups in your statement and if you just read it like this you should understand why it is not yielding a sensible result:
You say Give me the average of the maximum value for qty from tb_name, but since you are not grouping by anything there is only one maximum value.
If you need both values in one query, you can do so, but you need to have a grouping on your query for example like this:
select avg(qty), max(qty) from tb_name group by SomethingOtherThanQty
From your comments I guess you are indeed looking for a maximum of all averages over some group, for that a query with subquery could look like this:
SELECT MAX(AVERAGE) FROM
(SELECT
AVG(qty) AS AVERAGE
FROM tb_name
GROUP BY YourGroupingCriteria) AS SubQuery
maximum of the average is equal to the average if you only have one group, I imagine you can do a subquery in that you fetch average values of some GROUP and run a query on that that returns the maximum average of that subquery, but again what group are we talking about?You can do the following:
SELECT AVG(qty) FROM tb_name
This will give you the average value of quantity
OR
SELECT MAX(qty) FROM tb_name
This will give you the maximum value of quanity
Using both AVG and MAX on the same table is both logically and syntax-wise incorrect.