Skip to content
Advertisement

Need an SQL query that will left join with another table, which will in turn return the latest values based on time, grouped into a single row

I need help with a SELECT SQL query that will left join with another table on id column, which will in turn return the latest values based on time, grouped into a single row?

Basically, join the two tables in such a way that for each record in users table that exists in time_series table, return the latest values based on time grouped into a single row.

users table:

time_series table:

The result should look like this:

Advertisement

Answer

One option uses a couple of subqueries:

Another solution is to rank rows with row_number() for each column, giving priority to non-null values, and thenconditional aggregation:

User contributions licensed under: CC BY-SA
7 People found this is helpful
Advertisement