Skip to content
Advertisement

Finding latest timestamp per value across columns and rows

I have a database table with rows like

Where ID is the non-unique identifier for the rows and will be used to filter the initial dataset. Fields s1 to s6 contain values and columns ts1 to ts6 contain their timestamps. And I need to find the latest (or first) timestamp per “s” value. The “s” values are not unique and the same values might be in any of the “s” columns.

I’ve not tried anything yet, because I have no idea how to turn those columns into rows. If I knew, then I’d probably find the latest (with max function) per value. But how to turn them into rows? I understand there is PIVOT function, but I’m not entirely sure how to use it and how to turn those columns into single “s” and “ts” columns.

Edit:

Sample dataset:

Sample output:

I used the same unix epoch timestamp in the example, but this is much what the data looks like. It’s going to be about 20-100k rows per each dataset and 10-30 different values for “s” fields. Not sure if this is relevant or not.

Advertisement

Answer

Thank you @Conor Cunningham MSFT for the suggestion, this would be a good solution. I have reproed and got the expected results and posting it as the answer.

Source:

enter image description here

enter image description here

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