0

Having a very hard time wrapping my head around how this should be written, I have a key made up of a fixed number column and an incrementing number column.

I want to have the select query only return the newest row for each fixed number.

For example:

20, 1
20, 2
20, 3 <-- Should only return these rows
25, 1
25, 2 <--
30, 1
30, 2
30, 3 <--

Is there to write this in a single query without having to iterate over the results in php?

Maus
  • 3
  • 1
  • Do you only need to display those two columns, or are there other columns in the table that need to be output too? – Joe Sep 26 '13 at 16:25

2 Answers2

1

Try this::

select max(column1), column2
from table 
group by column2
Sashi Kant
  • 13,277
  • 9
  • 44
  • 71
  • This is it, you reversed the col names, but it works when switched. I knew it was something simple like this, thanks! – Maus Sep 26 '13 at 16:29
0
select col1, max(col2) from table group by col1

would give you

20 3
25 2
30 3

SqlFiddle Demo

Josh
  • 357
  • 1
  • 3
  • 18