Home > Enterprise >  How can I select a table skipping duplicated value postgreSQL
How can I select a table skipping duplicated value postgreSQL

Time:03-05

I have a table like this.

id grade_1 grade_2 createdAt
1 1 1 20220304
2 1 1 20220301
3 4 2 20220228

I want to select the current row(in here, id=1) and a row where the grade's value is different with the row I selected.(in here, id=3)

Like This

id grade_1 grade_2 createdAt
1 1 1 20220304
3 4 2 20220228

I tried to use subquery but it doesn't really worked for me. Is there any way to skip the duplicated value when selecting table?

CodePudding user response:

You can just do it with group by and a max value to retieve the one you want

SELECT 
    grade_1, 
    grade_2, 
    Max(createdAt) 
from 
    yourTable
Group by 
    grade_1, 
    grade_2

  • Related