Skip to content
Advertisement

need to get a subsequent record with a specific value

i have the following table :

For each status A i need to get the next status D. The result should be like this :

I have my own solution based on cross/outer apply , In terms of performance i need solution without cross/outer apply.

Advertisement

Answer

We can try using ROW_NUMBER here along with some pivot logic:

enter image description here

Demo

The idea here is to generate a row number sequence along your entire table, for each separate Status value (A or D). Then, aggregate by that row number sequence to bring the A and D records together.

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