Table Essentially looks like:
Serial-ID, ID, Date, Data, Data, Data, etc.
There can be Multiple Rows for the Same ID. I'd like to create a view of this table to be used in Reports that only shows the most recent entry for each ID. It should show all of the columns.
Can someone help me with the SQL select? thanks.
This seems like a good use for correlated subqueries:
Your date column would need to uniquely identify each row (like a
TIMESTAMP
type).There's about 5 different ways to do this, but here's one:
And here's another:
One more:
Ok, i'm getting carried away, here's the last one I'll post(for now):
I would use
DISTINCT ON
This works because
distinct on
suppresses rows with duplicates of the expression in parentheses.DESC
inorder by
means the one that normally sorts last will be first, and therefor be the one that shows in the result.https://www.postgresql.org/docs/10/static/sql-select.html#SQL-DISTINCT