473,405 Members | 2,334 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,405 software developers and data experts.

Invalid use of group function

18
Hi,

I'm not really sure if this is a PHP or Mysql issue, but I need some help!!

I have some php scripts to use with a mysql database. I've tested everything locally on my computer and they work fine, but when I put them on-line I get the message "Invalid use of group function". Here are the group function statements:

Expand|Select|Wrap|Line Numbers
  1. $query = 'SELECT id, name, total FROM totals where id IN(select max(id) from totals group by name) ORDER BY total DESC ';
  2.  
  3. $query = 'SELECT name, SUM(total) AS numtotal FROM totals group by name order by sum(total) desc';
Any ideas why they won't work on-line will be greatly appreciated!!

Thanks, Tom
Aug 12 '07 #1
8 19059
pbmods
5,821 Expert 4TB
Heya, Tom.

Please use CODE tags when posting source code. See the REPLY GUIDELINES on the right side of the page next time you post.

Which query is generating the error?
Aug 12 '07 #2
tom_b
18
Hi,

Sorry about the code tags. When I ran these queries through phpMyAdmin the first one worked fine, the second one generated this message;

#1111 - Invalid use of group function

Thanks, Tom
Aug 12 '07 #3
pbmods
5,821 Expert 4TB
Heya, Tom.

Expand|Select|Wrap|Line Numbers
  1. SELECT
  2.         `name`,
  3.         SUM(`total`)
  4.             AS `numtotal`
  5.     FROM
  6.         `totals`
  7.     GROUP BY
  8.         `name`
  9.     ORDER BY
  10.         `numtotal` DESC
  11.  
Note that since you're already naming your SUM() field, you can use that name in your ORDER BY clause.

From what I've read, MySQL generates this error when an aggregate function is used in a WHERE clause. Of course, you have no WHERE clause in your query, but I wonder if the error was caused by specifying a column alias for your aggregate function, but then not using the alias in the ORDER BY clause.
Aug 12 '07 #4
tom_b
18
Hi,

That did it!!! I THINK I understand your explanation, I guess the on-line server is a little fussier than running it locally. In any case it works perfectly, thank you very much!!

Tom
Aug 12 '07 #5
pbmods
5,821 Expert 4TB
Heya, Tom.

The thing about aggregate functions such as COUNT, MAX, etc., is that they are not actually calculated until the rest of the query is evaluated. For example, when you want to select the COUNT of all the rows in a table, MySQL will first compile all the rows from the table and then find the count.

This can cause some problems. For example, what if you only want to return results where the COUNT is greater than 3? Or expressed in code:
Expand|Select|Wrap|Line Numbers
  1. SELECT
  2.         `name`,
  3.         COUNT(`id`)
  4.             AS `count`
  5.     FROM
  6.         `Table`
  7.     WHERE
  8.         `count` > 3
  9.     GROUP BY
  10.         `name`;
  11.  
The problem here is that MySQL doesn't calculate the value of `count` until it compiles the entire result set. In other words, MySQL won't calculate `count` until it finds all rows where `count` is greater than three. Oops.

The solution to the above problem is to use the HAVING keyword, which is a special WHERE that gets applied after the initial result set gets calculated:
Expand|Select|Wrap|Line Numbers
  1. SELECT
  2.         `name`,
  3.         COUNT(`id`)
  4.             AS `count`
  5.     FROM
  6.         `Table`
  7.     GROUP BY
  8.         `name`
  9.     HAVING
  10.         `count` > 3;
  11.  
Or at least that's what "Invalid use of group function" means.

Of course, the problem that you were hitting was a little different. Simply put, you have to use a column name, instead of a calculated value, in the ORDER BY clause.

For example, in your query:
Expand|Select|Wrap|Line Numbers
  1. SELECT
  2.         `name`,
  3.         SUM(`total`)
  4.             AS `numtotal`
  5.     FROM
  6.         `totals`
  7.     GROUP BY
  8.         `name`
  9.     ORDER BY 
  10.         SUM(`total`) DESC
  11.  
You are trying to ORDER BY the value of the SUM of the `total` column (for example, ORDER BY 60 DESC). Which strikes MySQL as kind of silly, so it throws a silly error at you.

Now you might say, 'Hey, wait a second! I got it working before!'.

For example this works:
Expand|Select|Wrap|Line Numbers
  1. SELECT
  2.         `name`,
  3.         SUM(`total`)
  4.     FROM
  5.         `totals`
  6.     GROUP BY
  7.         `name`
  8.     ORDER BY 
  9.         SUM(`total`) DESC
  10.  
because MySQL is clever enough to see that you are SELECTing the SUM() as one of the columns, so it uses that column to order the results. However, once you rename the SUM() column, MySQL no longer recognizes it unless you use the alias that you assigned it.

At any rate, glad to hear you got it working! Good luck with your project, and if you ever need anything, post back anytime :)
Aug 12 '07 #6
tom_b
18
Hi,

Well, I certainly don't have the Mysql expertise you have, but your explanation does make sense. These are the kinds of things I learn every time I run into a new problem!! I'll certainly remember "having" in the future. As I said, everything worked locally, I'm now in the process of getting it on-line (first time using an on-line database!!), it will be interesting to see if any of the other pages have problems. If so I'd love to be able to run them by you!! Well, thanks again, I appreciate your time and effort to educate me a little!!

Tom
Aug 12 '07 #7
Thank to "pbmods" post #4

I had the same problem solve by you hint of the where clause.

that was hard to find with my 100 sql lines
Oct 4 '07 #8
clear explanation about 'having' clause, Thanks :)

@pbmods
Nov 15 '12 #9

Sign in to post your reply or Sign up for a free account.

Similar topics

3
by: moon | last post by:
Hi all, Im trying to execute this statement in SQL Plus but am getting the error not a single-group group function. My code is below. To explain what Im trying to get at, I want to return the...
2
by: Experienced but Undocumented | last post by:
Here's another one; thanks so much to everyone who helped with my last problem. I can't upgrade mySQL but I figured out a way around it from your examples. SELECT `subject` , max( `datetime` )...
1
by: Hollywood_Jack | last post by:
Peeps, I'm getting an "ORA-00937: not a single group group function error" when I try and run the following query. Any help is appreciated: SELECT tbt.major_cov_cd||', '|| tbt.claim_id||', '||...
2
by: bob_jenkins | last post by:
I'm looking for a term that means to group a function with its arguments into a thingy that can be pulled apart and executed later. There's lots of ways to do this, I'm just looking for the word...
7
by: tom_b | last post by:
Hi, I have some php scripts to use with a mysql database. I've tested them locally on my computer and they work fine, but when I put them on line I get the message "Invalid use of group...
1
by: bipinskulkarni | last post by:
How to count number of users created in a perticular year. ex.i want to count total number of users created in 2007using query.
4
by: luttkens | last post by:
I have a table salary, and when I select it, it looks like this Name Year Income ------------------------------- James Smith 2008 33000 James Smith 2007 32000 James Smith ...
0
by: jimgym1989 | last post by:
select ABS(ROUND((projectspecs.laborCost+projectspecs.contingencyCost + SUM(materialcost_details.qty*materialcost_details.unitCost))* (billing.accom/100) -...
4
by: sarah2855 | last post by:
I have a table that looks like this: CustomerID ProductOrdered OrderDate OrderQuantity 0001 ProdA 1/1/2007 3 0001 ProdA 1/1/2008 2 0002 ...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.