Based on the table called
How can I select records which have course name with latest date? I mean if I have two same course names for one ID, I should only show the latest one as the below result.
Simply, I want only to show the latest row per (“ID”, “Course Name”).
And what if I have two date columns in Course table, which are StartDate & EndDate and I want to show the same based on EndDate only.?
I am using PostgreSQL.
In PostgreSQL, to get unique rows for a defined set of columns, the preferable technique is generally
SELECT DISTINCT ON ("ID") *
ORDER BY "ID", "Course Date" DESC NULLS LAST, "Course Name";
Assuming you actually use those unfortunate upper case identifiers with spaces.
You get exactly one row per
ID this way – the one with the latest known
"Course Date" and the first
"Course Name" (according to sort order) in case of ties on the date.
You can drop
NULLS LAST if your column is defined
To get unique rows per
("ID", "Course Name"):
SELECT DISTINCT ON ("ID", "Course Name") *
ORDER BY "ID", "Course Name", "Course Date" DESC NULLS LAST;
Details in this related answer: