tags:

views:

98

answers:

6

The follow query is returning an error at column 143: "ORA-00934: group function is not allowed here"

SELECT * FROM 
TBLENTITYLOCATION TL INNER JOIN TBLENTITY TE ON TE.ENTITYID = TL.ENTITYID 
WHERE TE.ENTITYNAME = 'foobar' 
AND LOCATIONDATETIME = MAX(LOCATIONDATETIME)

It works fine if I take out the last line - AND LOCATIONDATETIME = MAX(LOCATIONDATETIME), but then it returns every instance of 'foobar' instead of just the latest. Can someone help me?

+2  A: 

You could try:

SELECT Top(1) * FROM  
TBLENTITYLOCATION TL INNER JOIN TBLENTITY TE ON TE.ENTITYID = TL.ENTITYID  
WHERE TE.ENTITYNAME = 'foobar'  
ORDER BY LOCATIONDATETIME DESC
Joel Etherton
I tried this but it says invalid identifier at "TOP". Is it because I'm using Oracle?
Dan
TOP only works in SQL. Check out http://windev.wordpress.com/2007/01/17/tip-of-the-day-t-sql-top-n-equivalent-for-oracle-and-mysql/
Gage
Oracle does not support the TOP syntax. The closest you could come to this is to use ROWNUM, but you'd need to nest the main select with the ORDER BY in a subquery, then apply the ROWNUM filter on top of that in order to ensure that you actually got the highest value.
Allan
Switched it to ROWNUM = 1 and it worked. Thank you both!
Dan
A: 

Take out MAX(LOCATIONDATETIME) from the where clause and put it in your select clause:

SELECT MAX(LOCATIONDATETIME) FROM TBLENTITYLOCATION TL INNER JOIN TBLENTITY TE ON TE.ENTITYID = TL.ENTITYID WHERE TE.ENTITYNAME = 'foobar'

Also add other fields to the select clause as selecting * is bad practise

Carnotaurus
I thought of this, but that would only return the DATETIME, instead of every column (attribute) of foobar
Dan
+3  A: 

Alternatively you can use a subquery in the WHERE clause like so:

SELECT * FROM 
TBLENTITYLOCATION TL INNER JOIN TBLENTITY TE ON TE.ENTITYID = TL.ENTITYID 
WHERE TE.ENTITYNAME = 'foobar' 
AND LOCATIONDATETIME = (SELECT MAX(LOCATIONDATETIME) FROM TBLENTITYLOCATION)
Ben Hoffstein
+1 - beat me to it ;o)
Andrew
If the max LOCATIONDATETIME from TBLENTITYLOCATION does not belong to a "foobar" entity, this query will return no rows.
Allan
Good point Allan - your answer is definitely the best one here :-)
Ben Hoffstein
A: 

Use the MAX in the Select statement

SELECT *, MAX(LOCATIONDATETIME) FROM TBLENTITYLOCATION TL INNER JOIN TBLENTITY TE ON TE.ENTITYID = TL.ENTITYID WHERE TE.ENTITYNAME = 'foobar'

Visionary Software Solutions
This is invalid SQL: 1) You can't use a wildcard column in an aggregate; 2) You can't use an aggregate function with non-aggregated columns without GROUP BY; 3) You can't use a wildcard column with additional columns without specifying the table (i.e. TBLENTITYLOCATION.*).
Allan
A: 

You could sort the Query descending, then just use the first row.

Sam
+6  A: 

You can't use an aggregate function (max) in a non-aggregated query. What you want is something like this:

SELECT * 
FROM TBLENTITYLOCATION TL 
INNER JOIN TBLENTITY TE 
ON TE.ENTITYID = TL.ENTITYID 
WHERE TE.ENTITYNAME = 'foobar' 
AND LOCATIONDATETIME = (select MAX(LOCATIONDATETIME)
                        FROM TBLENTITYLOCATION TL 
                        INNER JOIN TBLENTITY TE 
                        ON TE.ENTITYID = TL.ENTITYID 
                        WHERE TE.ENTITYNAME = 'foobar')

The basic rule is that, if you're using aggregate functions (i.e. min, max, avg, etc.), then all of the columns in the select statement must be in an aggregate function or a part of the GROUP BY clause. Even if you had the GROUP BY (which wouldn't do what you need, in this case) your original query would still be invalid because you can't reference aggregate functions in the WHERE clause. To filter by an aggregate function, you need to use the HAVING clause, which is applied after the results are aggregated (as opposed to WHERE, which is evaluated before).


Alternately, you could use ROWNUM and an ORDER BY clause to achieve the same result (essentially Oracle's version of TOP):

select *
from (SELECT tl.*, te.*, rownum as rn 
      FROM TBLENTITYLOCATION TL 
      INNER JOIN TBLENTITY TE 
      ON TE.ENTITYID = TL.ENTITYID 
      WHERE TE.ENTITYNAME = 'foobar'
      ORDER BY LOCATIONDATETIME DESC)
where rn = 1

It may look like you could collapse this down into a single select, but this is an illusion. If you were to do that the ROWNUM criteria would be applied before the ORDER BY, so you would get a semi-random row.

I believe the first version would be more efficient as it doesn't require that the results be sorted.

Allan
Worked like a charm, thanks!
Dan
+1: Best answer
OMG Ponies