StefanE
StefanE

Reputation: 7630

Get max one result of each value in a column

I'm working with a Informix DB and with a table like this:

Col1 Col2 Col3
a    a    a
b    a    c
c    b    a
d    c    d

Is it possible from the SQL statement to just display ONE of the first 2 rows by just specify only unique results (values can be anything). I only want one result from col2 with the same value and I don't mind which one of the lines being retrieved.

I hope I'm making sense.

Upvotes: 1

Views: 213

Answers (1)

RichardTheKiwi
RichardTheKiwi

Reputation: 107766

Assuming you have a tie-breaking column (a single-column primary key), you could use something like this

select t.*
from (
    select col2, min(pk_id) pk_id
    from tbl
    group by col2
) x, tbl t
where t.col2=x.col2 and t.pk_id=x.pk_id

Upvotes: 1

Related Questions