weardstuff
weardstuff

Reputation: 807

DISTINCT on multiple columns

I have, SELECT DISTINCT (first),second,third FROM table

AND i want not only the first to be DISTINCT and the second to be DISTINCT to but the third to stay without DISTINCT , i tryed like that.

SELECT DISTINCT (first,second),third FROM table

And couple more things but didnt worked.

Upvotes: 6

Views: 2925

Answers (2)

Quassnoi
Quassnoi

Reputation: 425251

SELECT  m.first, m.second, m.third -- and possibly other columns
FROM    (
        SELECT  DISTINCT  first, second
        FROM    mytable
        ) md
JOIN    mytable m
ON      m.id =
        (
        SELECT  id
        FROM    mytable mi
        WHERE   mi.first = md.first
                AND mi.second = md.second
        ORDER BY
                mi.first, mi.second, mi.third
        LIMIT 1
        )

Create an index on (first, second, third) for this to work fast.

Upvotes: 6

lhan
lhan

Reputation: 4635

Have you seen this post?

Select distinct from multiple fields using sql

They seem very similar, maybe you could try something like that?

Hope this helps!

Upvotes: 0

Related Questions