If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

 
Go Back  dBforums > Database Server Software > MySQL > Invalid use of group function

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 08-05-10, 00:49
skl25 skl25 is offline
Registered User
 
Join Date: Aug 2010
Posts: 3
Invalid use of group function

I am working on a code that has the following requirements:

Delete the shipments with quantity less than the average for each project.

I have written the code

DELETE FROM Shipments
WHERE quantity < AVG(quantity);

however I get the ERROW 1111 (HY000): Invalid use of group function.

Any help would be greatly appreciated.
Reply With Quote
  #2 (permalink)  
Old 08-05-10, 01:15
r937 r937 is offline
SQL Consultant
 
Join Date: Apr 2002
Location: Toronto, Canada
Posts: 19,525
Quote:
Originally Posted by skl25 View Post
Delete the shipments with quantity less than the average for each project.
nice homework assignment

you need to select the average in a subquery

__________________
r937.com | rudy.ca
please visit Simply SQL and buy my book
Reply With Quote
  #3 (permalink)  
Old 08-05-10, 01:21
skl25 skl25 is offline
Registered User
 
Join Date: Aug 2010
Posts: 3
Haha thanks. It makes me mad because I visualize how this works but I can't seem to get the average function to cooperate!

DELETE FROM Shipments
WHERE quantity < (SELECT AVG(quantity)
FROM Shipments);

That is what I have rewritten, however I get a new error message saying

ERROR 1093 (HY000): You can't specify target table 'Shipments' for update in FROM clause.

This makes sense to me since you keep updating the avg, but how else would you write it? Thanks again for your help!
Reply With Quote
  #4 (permalink)  
Old 08-05-10, 03:41
gvee gvee is offline
www.gvee.co.uk
 
Join Date: Jan 2007
Location: UK
Posts: 10,156
Perhaps the average value could be stored in a variable?
__________________
George
Twitter | Blog
Reply With Quote
  #5 (permalink)  
Old 08-05-10, 03:56
mike_bike_kite mike_bike_kite is offline
vaguely human
 
Join Date: Jun 2007
Location: London
Posts: 2,519
Try reading up on correlated subqueries.
__________________
Mike
Reply With Quote
  #6 (permalink)  
Old 08-05-10, 04:57
r937 r937 is offline
SQL Consultant
 
Join Date: Apr 2002
Location: Toronto, Canada
Posts: 19,525
Quote:
Originally Posted by mike_bike_kite View Post
Try reading up on correlated subqueries.
and how does a sql server documentation page help this particular mysql scenario?

george is right, skl25, you'll need to use a mysql user variable

MySQL :: MySQL 5.0 Reference Manual :: 8.4 User-Defined Variables
__________________
r937.com | rudy.ca
please visit Simply SQL and buy my book
Reply With Quote
  #7 (permalink)  
Old 08-05-10, 12:55
Danielle24 Danielle24 is offline
Registered User
 
Join Date: Aug 2010
Location: Deer Park, Texas
Posts: 1
the average value could be stored in a variable?
Reply With Quote
  #8 (permalink)  
Old 08-05-10, 13:08
r937 r937 is offline
SQL Consultant
 
Join Date: Apr 2002
Location: Toronto, Canada
Posts: 19,525
Quote:
Originally Posted by Danielle24 View Post
the average value could be stored in a variable?
you catch on pretty quick
__________________
r937.com | rudy.ca
please visit Simply SQL and buy my book
Reply With Quote
  #9 (permalink)  
Old 08-05-10, 13:30
mike_bike_kite mike_bike_kite is offline
vaguely human
 
Join Date: Jun 2007
Location: London
Posts: 2,519
Quote:
Originally Posted by r937 View Post
and how does a sql server documentation page help this particular mysql scenario?

george is right, skl25, you'll need to use a mysql user variable
It was just to show him how such queries work - I had no desire to do his homework for him. I will admit that I'd forgotten that MySQL won't allow a correlated query within a delete - shame as most other RDBMs do. I still wouldn't use variables though (I'd go for a temp table) or are you aiming for a NZDF style solution.
__________________
Mike
Reply With Quote
  #10 (permalink)  
Old 08-05-10, 13:52
skl25 skl25 is offline
Registered User
 
Join Date: Aug 2010
Posts: 3
Thanks everyone for your advice!
Reply With Quote
  #11 (permalink)  
Old 08-05-10, 21:08
Pat Phelan Pat Phelan is offline
Resident Curmudgeon
 
Join Date: Feb 2004
Location: In front of the computer
Posts: 12,606
Quote:
Originally Posted by r937 View Post
and how does a sql server documentation page help this particular mysql scenario?
The SQL Server error message ties them together, at least indirectly.

-PatP
__________________
In theory, theory and practice are identical. In practice, theory and practice are unrelated.
Reply With Quote
  #12 (permalink)  
Old 08-05-10, 22:20
r937 r937 is offline
SQL Consultant
 
Join Date: Apr 2002
Location: Toronto, Canada
Posts: 19,525
um, which SQL Server error message?

the page mike linked to talked about correlated subqueries

the only error message in this thread was a mysql one

your reply seems to have had a NBDF (nothing but deviosity factor)

__________________
r937.com | rudy.ca
please visit Simply SQL and buy my book
Reply With Quote
Reply

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On