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 > Syntax check for GROUP BY

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 01-27-04, 09:58
electroniceric electroniceric is offline
Registered User
 
Join Date: Apr 2003
Posts: 3
Syntax check for GROUP BY

Hi,
I'm having trouble with this query:

SELECT Max(DATE_ADD('1970-1-1',INTERVAL Lastupdate/3600/24 DAY)) as BaskDate, ofm_baskets.Code, Sum(ofm_baskets.Quantity) AS TotalQty
FROM (ofm_basklist INNER JOIN ofm_basklupd ON ofm_basklist.Session_id = ofm_basklupd.Session_id) INNER JOIN ofm_baskets ON ofm_basklist.Basket_id = ofm_baskets.Id
GROUP BY ofm_baskets.Code
HAVING (((ofm_basklist.Order_id)=0))
ORDER BY Max(DATE_ADD('1970-1-1',INTERVAL Lastupdate/3600/24 DAY));

I get the following error:
ERROR 1111: Invalid use of group function

Can someone give me an idea of what's wrong with this query? I'm using MySQLd version 4.0.16.

Thanks,

Eric
Reply With Quote
  #2 (permalink)  
Old 02-02-04, 05:01
ArturJ ArturJ is offline
Registered User
 
Join Date: Feb 2004
Posts: 3
Re: Syntax check for GROUP BY

Quote:
Originally posted by electroniceric
Hi,
I'm having trouble with this query:

SELECT Max(DATE_ADD('1970-1-1',INTERVAL Lastupdate/3600/24 DAY)) as BaskDate, ofm_baskets.Code, Sum(ofm_baskets.Quantity) AS TotalQty
FROM (ofm_basklist INNER JOIN ofm_basklupd ON ofm_basklist.Session_id = ofm_basklupd.Session_id) INNER JOIN ofm_baskets ON ofm_basklist.Basket_id = ofm_baskets.Id
GROUP BY ofm_baskets.Code
HAVING (((ofm_basklist.Order_id)=0))
ORDER BY Max(DATE_ADD('1970-1-1',INTERVAL Lastupdate/3600/24 DAY));

I get the following error:
ERROR 1111: Invalid use of group function

Can someone give me an idea of what's wrong with this query? I'm using MySQLd version 4.0.16.

Thanks,

Eric
May be you need use 2 group parameters?
For example, GROUP BY ofm_baskets.Code, TotalQty
Reply With Quote
  #3 (permalink)  
Old 02-02-04, 07:21
r937 r937 is online now
SQL Consultant
 
Join Date: Apr 2002
Location: Toronto, Canada
Posts: 19,534
what is the datatype of Lastupdate? what are some sample values in that column?

your GROUP BY is correct assuming that the expression involving Lastupdate is correct

rewrite the HAVING condition as a WHERE condition
__________________
r937.com | rudy.ca
please visit Simply SQL and buy my book
Reply With Quote
  #4 (permalink)  
Old 02-04-04, 13:54
electroniceric electroniceric is offline
Registered User
 
Join Date: Apr 2003
Posts: 3
Aha!
Currently Lastupdate is varchar(10), but that was not actually the source of the problem. The problem turns out to be attempting to use the grouping function Max in the ORDER BY clause.

After changing the query to:
SELECT Max(DATE_ADD('1970-1-1',INTERVAL Lastupdate/3600/24 DAY)) as BaskDate, ofm_baskets.Code, Sum(ofm_baskets.Quantity) AS TotalQty
FROM (ofm_basklist INNER JOIN ofm_basklupd ON ofm_basklist.Session_id = ofm_basklupd.Session_id) INNER JOIN ofm_baskets ON ofm_basklist.Basket_id = ofm_baskets.Id
WHERE (ofm_basklist.Order_id)=0
GROUP BY ofm_baskets.Code
ORDER BY BaskDate;

Now it works perfectly.

I cut my teeth on Access/SQL SERVER queries, and my original query is how Access does ORDER BY clauses, so beware of that if you go from one to the other.

Many thanks for the helpful suggestions.
Reply With Quote
  #5 (permalink)  
Old 02-04-04, 13:57
r937 r937 is online now
SQL Consultant
 
Join Date: Apr 2002
Location: Toronto, Canada
Posts: 19,534
you're right, and i knew that too! sorry

can't use the aggregate in the ORDER BY in mysql, can't use the alias in sql server
__________________
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