Skip to content
Advertisement

Comparing two column values in postgres

I have table having following columns and records. I need to compare the two column values(ColumnA and ColumnB), if ColumnB>ColumnA then and update the third column from ‘N’ TO ‘Y’

CREATE TABLE Test(ColumnA int,ColumnB int,Result Varchar(2))

INSERT INTO Test values(1,3,'N')
INSERT INTO Test values(2,1,'N')
INSERT INTO Test values(1,5,'N')
INSERT INTO Test values(8,7,'N')

I need to update Result Column=’Y’ for first and third row because columnB>ColumnA

Result

ColumnA   ColumnB Result
1          3      Y
2          1      N
1          5      Y
8          7      N

Advertisement

Answer

update test
set result = 
    case
        when columnb > columna then 'Y'
        else 'N'
    end;

Hope this will help.

User contributions licensed under: CC BY-SA
10 People found this is helpful
Advertisement