I would propose a clean version based on DISTINCT ON
(see docs):
SELECT DISTINCT ON (usr_id)
time_stamp,
lives_remaining,
usr_id,
trans_id
FROM lives
ORDER BY usr_id, time_stamp DESC, trans_id DESC;
More Related Contents:
- Optimize groupwise maximum query
- SELECT DISTINCT is slower than expected on my table in PostgreSQL
- Select first row in each GROUP BY group?
- How to concatenate strings of a string field in a PostgreSQL ‘group by’ query?
- How to use RETURNING with ON CONFLICT in PostgreSQL?
- PostgreSQL DISTINCT ON with different ORDER BY
- Split comma separated column data into additional columns
- How to delete duplicate entries?
- Escaping keyword-like column names in Postgres
- Transpose latest rows per user to columns
- Delete duplicate rows (don’t delete all duplicate)
- GROUP BY + CASE statement
- subquery in FROM must have an alias
- Compute percents from SUM() in the same SELECT sql query
- SQL Between clause with strings columns
- Insert an image in postgresql database
- Perform this hours of operation query in PostgreSQL
- Getting date list in a range in PostgreSQL
- Postgres Dynamic Query Function
- How do I delete a fixed number of rows with sorting in PostgreSQL?
- Does the JDBC spec prevent ‘?’ from being used as an operator (outside of quotes)?
- How to use an ALIAS in a PostgreSQL ORDER BY clause?
- Postgresql Column Not Found, But Shows in Describe
- PostgreSQL does not accept column alias in WHERE clause
- Why can’t I use column aliases in the next SELECT expression?
- Postgres window function and group by exception
- SQL LIKE condition to check for integer?
- Counting the number of occurrences of a substring within a string in PostgreSQL
- Temporal database design, with a twist (live vs draft rows)
- How to bulk insert only new rows in PostreSQL