I'm trying to get the maximum number to "max"
getting error:
Error Code: 1111. Invalid use of group function 0.000 sec
SELECT max(count(*)) as max
FROM ticket
group by fan_fan_id;
I'm not sure what is the problem here and I will be glad to get some help here - also I need to solve it without "limit 1" option
SQL does not allow nesting aggregate functions like the example you show.
The argument to an aggregate function must be a scalar expression, not an aggregate expression.
You can do what you want this way:
SELECT MAX(c) FROM (SELECT COUNT(*) AS c FROM ticket GROUP BY fan_fan_id) AS t;
Or an alternative is to sort by value descending, and return only the first count:
SELECT COUNT(*) AS c FROM ticket GROUP BY fan_fan_id ORDER BY c DESC LIMIT 1;
Related
I written the query without ISNULL Function in mysql
SELECT DISTINCT s.st_symbol,f.symbol,f.closeprice,f.ltp,f.prevclprice AS CLOSE,(((LTP-prevclprice)/(prevclprice*100))) AS per_change,f.expdate
FROM stmp_stocks_qty AS s
JOIN tbl_intraday_nsefoprice_latest AS f ON s.st_symbol = f.symbol AND f.ID IN (SELECT MAX(ID) FROM tbl_intraday_nsefoprice_latest GROUP BY SYMBOL)
ORDER BY s.st_symbol,f.ExpDate DESC,f.createdon DESC;
This query is working fine and while I execute this query I get the null values in my column based on the calculation "(((LTP-prevclprice)/(prevclprice*100)))" .
ex:
To avoid this null value I used ISNULL Function and written a query like
SELECT DISTINCT s.st_symbol,f.symbol,f.closeprice AS CLOSE,ISNULL(((LTP-prevclprice)/(prevclprice*100)),0) AS per_change,f.expdate
FROM stmp_stocks_qty AS s
JOIN tbl_intraday_nsefoprice_latest AS f ON s.st_symbol = f.symbol AND f.ID IN (SELECT MAX(ID) FROM tbl_intraday_nsefoprice_latest GROUP BY SYMBOL)
ORDER BY s.st_symbol,f.ExpDate DESC,f.createdon DESC;
But while I executing this query I'm getting this king of error
Query: SELECT DISTINCT s.st_symbol,f.symbol,f.closeprice AS CLOSE,isnull(((LTP-prevclprice)/(prevclprice*100)),0) AS per_change,f.expd...
Error Code: 1582
Incorrect parameter count in the call to native function 'isnull'
Execution Time : 0 sec
Transfer Time : 0 sec
Total Time : 0 sec
---------------------------------------------------
Here my intension is if any column is passing null value into the column it should became a 0.
Suggest me how can I achieve this without error.
I'm new to mysql
(Sorry for my bad english)
The MySQL equivalent of ISNULL is IFNULL
or
you can use coalesce(), so your query will be:
SELECT DISTINCT s.st_symbol,f.symbol,f.closeprice AS CLOSE, coalesce(((LTP-prevclprice)/(prevclprice*100)),0) AS per_change,f.expd...
ISNULL() is a function which accepts only one argument. Function tests does the argument IS NULL and returns TRUE (1) or FALSE (1) depends on the testing result.
The function which accepts 2 arguments and returns first value if it is not NULL and second value otherwise is IFNULL().
I'm attempting to sort an aggregate column, which contains some zero values. I need the zero values to be last.
For non-aggregate columns I can do something like this (simplified example query):
SELECT age FROM books
ORDER BY
age = 0,
age ASC
However, for aggregate columns I'm getting an error as the column doesn't exist:
SELECT avg(age) as avg_age FROM books
GROUP BY book.type
ORDER BY
avg_age = 0,
avg_age ASC
The error is:
SQLSTATE[42S22]: Column not found: 1247 Reference 'avg_age' not supported (reference to group function)
I totally appreciate why this is happening, but I wasn't able to find a workaround, any tips?
There seams to be a (old) related bug report
[21 Mar 2016 9:22] Jiří Kavalík
Description: When using alias to aggregated column in ORDER BY only
plain alias is allowed, using it in any expression returns error.
http://sqlfiddle.com/#!9/e87bb/7
Workarounds:
- select the expression and use its alias
- use a derived table and order the outer one
How to repeat: create table t(a int);
-- these work select sum(a) x from t group by a order by x; select sum(a) x from t group by a order by sum(a); select sum(a) x from t
group by a order by -sum(a);
-- this one wrongly gives "Reference 'x' not supported (reference to group function)" select sum(a) x from t group by a order by -x;
source
You would have to write, this is better as the query is then also ANSI/ISO SQL standard valid meaning the query is most likely better portable between most databases vendor software.
SELECT
avg(books.age) as avg_age
FROM books
GROUP BY books.type
ORDER BY
avg(books.age) = 0
, avg(books.age) ASC
see demo this bug is fixed in MySQL 8.0 see demo
Try repeating the code
SELECT avg(age) as avg_age
FROM books
GROUP BY book.type
ORDER BY avg(age) = 0, avg(age) ASC
I have this code:
#people.count
Which produces a query beginning with:
SELECT COUNT(*) FROM ( SELECT * .....
And this error:
column "peoplerelationships.updated_at" must appear in the GROUP BY clause or be used in an aggregate function
LINE 5: ORDER BY peoplerelationships.updat...
If I query just #people, there is no error. By requesting a COUNT it introduces an aggregate function to the query and hence the error. Can you ensure #people is evaluated first, and then counted? How do you do this properly?
I'm moving my Delphi application from MySQL to SQL server 2012. In MySQL I had this query:
SELECT *,(XS+S+M+L+XL+XXL+[1Size]+Custom) as Total FROM StockData
GROUP BY StyleNr,Customer,Color
ORDER BY StyleNr,Customer,Color
And it worked perfectly. But in Microsoft SQL Server 2012 this query says
Msg 8120, Level 16, State 1, Line 1
Column 'StockData.ID' is invalid in the select list because it is not contained in either an aggregate function or the GROUP BY clause.
If I change my query to:
SELECT *,([XS]+[S]+[M]+[L]+[XL]+[XXL]+[1Size]+[Custom]) total
FROM [dbo].[stockdata]
GROUP BY ID,StyleNr,Customer,Color
ORDER BY StyleNr,Customer,Color
Then I get this error:
Msg 8120, Level 16, State 1, Line 1
Column 'dbo.stockdata.XS' is invalid in the select list because it is not contained in either an aggregate function or the GROUP BY clause.
Any ideas?
Here is the table's design view:
SQL Server is working as expected. You must include all items in your SELECT list in either a GROUP BY or in an aggregate function:
SELECT *,(XS+S+M+L+XL+XXL+[1Size]+Custom) as Total
FROM StockData
-- GROUP BY ID,StyleNr,Customer,Color, XS,S,M,L,XL,XXL,[1Size],Custom
ORDER BY StyleNr,Customer,Color
Or you might be able to use:
SELECT StyleNr,Customer,Color, SUM(XS+S+M+L+XL+XXL+[1Size]+Custom) as Total
FROM StockData
GROUP BY StyleNr,Customer,Color
ORDER BY StyleNr,Customer,Color;
All columns in an aggregate query must either be used by an aggregate function or a group by. Try only selecting the columns you require rather than * I.e. select stylenr, customer, color, ([...] ) as Total from.
This is a SQL standard way of dealing with aggregates, you'd get a similar error in Oracle.
You can also use this approach:
with OrdinalOnGroup
(
SELECT
Ordinal = rank() over(partition by StyleNr, Customer, Color order by id)
, *, (XS+S+M+L+XL+XXL+[1Size]+Custom) as Total
FROM StockData
)
select *
from OrdinalOnGroup
where Ordinal = 1;
PARTITION BY denotes the grouping of related information, this is called windowing
select max( sum(duration) ),cd from rent group by cd;
.
ERROR 1111 (HY000): Invalid use of group function
From documentation - group (aggregate) functions that operate on sets of values.. SUM returns scalar value.
Is this what you want?
SELECT MAX(duration_sum_by_cd) FROM (
SELECT SUM(duration) duration_sum_by_cd FROM rent
GROUP BY cd;
) t
that query is very broken. firstly, i don't think you can put a max around sum... 2nd you are grouping on a column "cd" which isn't in the selected columns.
I suggest doing some/many tutorials from here