tech_geek
tech_geek

Reputation: 1854

Return distinct values from multiple columns in one query

i have searched but i did not find any good answer actually i got the distinct value but the problem is i am applying query on two columns it should return distinct values but it is returning these values

Au |FAA303
Au |FAA505

From my table i want to appear Au only one time as it is now associated with the FAA303 and FAA505

What i want is like this

Au |FAA303
   |FAA505

This is my query in postgresql. I am kinda new to the database queries.

select distinct column1, column2
from table_name

Upvotes: 0

Views: 3596

Answers (1)

trincot
trincot

Reputation: 351359

The distinct keyword applies to the combination of all selected fields, not to the first one only.

Suppressing repeated values is something you would typically do in an application that connects to your database and performs the query.

Just to show you that it is possible in SQL, I provide you this query, but please consider doing this in the application instead:

select  case row_number() over (partition by column1 order by column2)
            when 1 then column1
        end as column1,
        column2
from (
    select   distinct column1,
             column2
    from     table_name
    order by column1, column2
)

Upvotes: 4

Related Questions