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
Related
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;
I have a table as follows:
log (log_id, log_success (bool), log_created)
I would like to SELECT and return 3 columns date success and no_success, where the former does not exist in table and finally aggregate them by day.
I have created this query:
SELECT
log_created as 'date'
COUNT(*) AS 'count',
SUM(log_success) AS 'success'
SUM('count' - 'success') AS 'no_success'
FROM send_log
GROUP BY DATE_FORMAT(log_created, '%Y-%m-%d');
Would I be able to achieve it with this query? Is my syntax correct?
Thanks.
You can't reuse an alias defined in the select within the same select clause. The reason for this is that it might not even have been defined when you go to access it. But, you easily enough can repeat the logic:
SELECT
log_created AS date,
SUM(log_success) AS success,
COUNT(*) - SUM(log_success) AS no_success,
FROM send_log
GROUP BY
log_created;
I don't know why you are calling DATE_FORMAT in the group by clause of your query. DATE_FORMAT is usually a presentation layer function, which you call because you want to view a date formatted a certain way. Since it appears that log_created is already a date, there is no need to call DATE_FORMAT on it when aggregating. You also should not even need in the select clause, because the default format for a MySQL date is already Y-m-d.
You must select DATE_FORMAT(log_created, '%Y-%m-%d') if you want to group by this.
Also you can get the no_success counter with SUM(abs(log_success - 1))
SELECT
DATE_FORMAT(log_created, '%Y-%m-%d') date,
SUM(log_success) log_success,
SUM(abs(log_success - 1)) no_success
FROM send_log
GROUP BY date;
See the demo
Is there any way to avoid repeating column expressions in the SELECT query? I want to divide the sum and count of a column but would like to use the assigned name instead of repeating SUM(value)/COUNT(value) or using a sub query. Is this possible? If so, does that speed up the query by not repeating the calculation of the sum and count or does mysql remember already calculated expressions?
SELECT datalist.type, SUM(value) AS type_sum, COUNT(value) AS type_count, type_sum/type_count
FROM (...) AS datalist
GROUP BY datalist.type
throws: #1054 - Unknown column 'type_sum' in field list
Unless you put it in outer query, this is the only way.
SELECT datalist.type, SUM(value) AS type_sum, COUNT(value) AS type_count, SUM(value)/COUNT(value)
FROM (...) AS datalist
GROUP BY datalist.type
One workaround would be to use a alias table with pre-defined calculations and then later call it from outer table such as:
select d.type_sum/d.type_count as dividedValue from (SELECT datalist.type, SUM(value)
AS type_sum, COUNT(value) AS type_count
FROM (...) )AS d
GROUP BY d.type
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
I'm trying to understand COUNT(*), and therefore I created a testing query:
SELECT COUNT(*)
WHERE COUNT(UITLENINGEN.LLNR) >= 30;
When I click Execute, I get the following error:
Syntax error (operator missing) in query-expression COUNT(*) WHERE COUNT(UITLENINGEN.LLNR) >= 30.
What am I doing wrong?
Try this
SELECT COUNT(*) FROM UITLENINGEN GROUP BY LLNR HAVING COUNT(UITLENINGEN.LLNR) >= 30;
I don't understand what you're trying to do. The query below is based on a table which includes a field named category_id. And it uses GROUP BY category_id to count the number of rows within each such group. The HAVING clause limits the result set to only those groups whose count is at least 30.
SELECT category_id, COUNT(*)
FROM YourTable
GROUP BY category_id
HAVING COUNT(*) >= 30;
If that is nothing like what you're trying to accomplish, please give us more detailed information so we may better understand your situation. A brief set of sample data, and the output you want based on that sample, would help tremendously.
You have not specified the table from which the data should be retrieved. Try the following
SELECT COUNT(*) from tableName
WHERE COUNT(UITLENINGEN.LLNR) >= 30;
Add your table name to the query.
SELECT COUNT(*) FROM UITLENINGEN WHERE COUNT(UITLENINGEN.LLNR) >= 30;
Please, add table name and use having statement where aggregation funcion required. E.g.:
select count(*)
from UITLENINGEN
having count(UITLENINGEN.LLNR) >= 30;