Here’s what my table TheTable looks like
ColA | ColB | ColC
------+-------+------
abc | 2005 | item7
abc | 2010 | item3
def | 2009 | item2
def | 2010 | item4
def | 2011 | item5
def | 2012 | item1
And I want to write a query to return this result:
ColA | ColB | ColC
------+-------+------
abc | 2010 | item3
def | 2012 | item1
I ended up with:
SELECT TheTable.ColA,TheTable.ColB,TheTable.ColC
FROM TheTable,(SELECT ColA a,MAX(ColB) b FROM TheTable GROUP BY ColA) t
WHERE t.a=TheTable.ColA AND t.b=TheTable.ColB
But I’m not sure this kind of nested query is very efficient. So the question is, is there a better way?
1 Answer