Re: GROUP BY clause not working



Lorenzo Thurman wrote:
I'm using PHP 5 to make an ODBC connection to an MS Access database
using PEAR and I'm getting DB Error: Syntax Error when using the GROUP
BY clause. Here's my query:
SELECT * FROM HardwareInstallationsSummary WHERE LocationID = $loc_ID
GROUP BY EquipmentTypeID


In standard SQL, you cannot use columns in a query containing a GROUP BY
clause, unless they are part of the clause or an aggregate function.

Examples:

SELECT LocationID FROM HardwareInstallationsSummary WHERE LocationID =
$loc_ID
GROUP BY EquipmentTypeID,LocationID;

SELECT count(*) FROM HardwareInstallationsSummary WHERE LocationID = $loc_ID
GROUP BY EquipmentTypeID;

In the last example, the wildcard match is accepted, because it's used in an
aggregate function (count).

Note that there are some DBMS's which allow you to break this rule, like
MySQL.


JW


.



Relevant Pages

  • Re: problem to SELECT using Microsoft Access (posted again, because of posting error)
    ... I tested that query. ... I get an error "syntax error in from clause" which is ... becuase Table is a reserved word ...
    (borland.public.delphi.database.ado)
  • Re: probably not as complex as Im thinking...
    ... No, sorry, I get a syntax error in Group By clause here, and I tried not ... >> OK, I have 4 queries, the SQL view of which I have below, with brief ... Please understand that I use the Query Design Grid to make ...
    (microsoft.public.access.queries)
  • Re: GROUP BY clause not working
    ... using PEAR and I'm getting DB Error: Syntax Error when using the GROUP BY clause. ... Here's my query: ... SELECT * FROM HardwareInstallationsSummary WHERE LocationID = $loc_ID GROUP BY EquipmentTypeID ...
    (comp.lang.php)
  • Re: VB MS Access Syntax error in FROM clause Error
    ... in a query are reserved words. ... "VB MS Access Syntax error in FROM clause" wrote in ... >FROM clause." ...
    (microsoft.public.data.ado)
  • Re: MIN - returns nothing?
    ... explains why my query was working on some tests but not ... > WHERE filters before aggregates are calculated. ... > GROUP BY LocationID ... > quicker response by posting to the newsgroup. ...
    (microsoft.public.inetserver.asp.db)