Skip to content
Advertisement

Manipulate data in SQL (backfilling, pivoting)

I have a table similar to this small example:

![enter image description here

I want to manipulate it to this format:

enter image description here

Here’s a sample SQL script to create an example input table:

I tried using combination of LAG and CASE WHEN, not successful so far. Looking for some ideas on how to manipulate (what functions etc). Would be awesome to see example script for the manipulation.

Where there is multiple values per id & hr, then earliest values to be used. E.g. id=1 & hr=7, then hr_7=uses value from 11:50. Although in this example, it’s the same values for both records, it can differ.

Advertisement

Answer

I would suggest this logic:

This first removes the duplicates and then adds a range for when the score is valid. The conditional aggregation then uses this information.

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