I have two tables: A and B
A
provider_id | date |
---|---|
111 | date |
222 | date |
333 | date |
B
provider_id | status | date |
---|---|---|
111 | initialized | date |
111 | released | date |
222 | failed | date |
The result I want
provider_id | status | date |
---|---|---|
111 | released | A date |
222 | failed | A date |
333 | null | A date |
Among the things I tried is the left join
select * from "A" left join "B" on "B"."provider_id" = "A"."provider_id" order by "A"."date" desc;
But I got the duplicated records based on status
provider_id | status | date |
---|---|---|
111 | initialized | date |
111 | released | date |
222 | failed | date |
333 | null | date |
2
Answers
To sort on the A date, you can join B with a subquery
Use
distinct on
to retrieve only one record perprovider_id
andorder by
to specify that this shall be the most recent (latest) one. More info