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 DISTINCT, and the third to be left without DISTINCT, I tried like this.

 SELECT DISTINCT (first,second),third FROM table 

And a couple more things, but it didn’t work.

+6
source share
2 answers
 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) so that it works quickly.

+6
source

Have you seen this post?

Choose different than multiple fields using sql

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

Hope this helps!

0
source

Source: https://habr.com/ru/post/886796/


All Articles