I have a table with 6 columns: id
, a
, b
, c
, d
, e
. id
is primary key.
I am trying to retrieve distinct a, b, c, max(d) for that group, and e that is present in the same row as max(d) is (column "id" is not relevant for my query).
I tried this query:
SELECT a, b, c, MAX(d), e
FROM tablename
GROUP BY a, b, c;
but it gives me "Invalid expression in the select list (not contained in either an aggregate function or the GROUP BY clause).". If I add an extra GROUP BY e, it just gives me distinct a, b, c, e, with a MAX(d) for each, which is not what I need. I do understand why this happens, what I don't understand is how to make it do what I need...
Is a subquery the way to go? Could you write one for me?
The most frustrating thing is that my query would work in MySQL :(