Binu V Pillai
Binu V Pillai

Reputation: 306

Remove duplicate values from the column of a PostgreSQL table and maintain only one value per column for duplicated rows

I would like to format the data of below table as follows. Here on the value column, I want to maintain only on value for each of the duplicated rows.

input table

code    value
A       10
A       10
A       10
B       20
B       20
B       20
C       30
C       30
D       40

Expected result

code    value
A        10
A   
A   
B        20
B   
B   
C        30
C   
D        40

Upvotes: 0

Views: 85

Answers (1)

Radim Bača
Radim Bača

Reputation: 10701

A combination of CASE and window function can solve your problem

select code, 
       case when t.rn = 1 then value else null end value
from (
    select row_number() over (partition by code, value order by value) rn,
           code, value
    from your_table
) t

Upvotes: 1

Related Questions