let’s assume we have two tables
table1
----------------------
|ID | Date |
----------------------
|1 |20110101|
|1 |20110102|
|1 |20110103|
|2 |20110102|
|2 |20110103|
|2 |20110104|
----------------------
table2
----------------------
|ID2 |val |
----------------------
|1 |152 |
|2 |155 |
----------------------
Using this query
SELECT * FROM table1, table2
WHERE table1.ID = table2.ID2
GROUP BY table1.ID
ORDER BY DATE DESC
Mysql should return this
-------------------------------------------
|ID |date |ID2 |val |
-------------------------------------------
|1 |20110103|1 |152 |
|2 |20110104|2 |155 |
-------------------------------------------
In Oracle I get this error:
ORA-00979: not a GROUP BY expression
EDIT:
The MAX function on the column Date does not work because this column is varchar(200)
The database/tables structure is not mine and I cannot alter it.
You need to do one of two things…
In your case, you don’t just want a MAX() from table1, as it may be possible that a higher
idhas a lowerdate. In that case, I’d be inclined to use a lookup system…NOTE: This assumes your date is formatted such that alphanumeric ordering WILL yield the correct date order. If that is NOT the case (and
d-m-yyyywill not order correctly), you need to replacedate_fieldwithTO_DATE(date_field)to ensure the correct order.NOTE: Use of
TO_DATE(date_field)will also probably fix your MAX() problems.NOTE: If you want to store dates as strings, but them to be order friendly, use
yyyy-mm-dd