I have a table (time_period)
The table lists several time periods like so.
unique_id type_id sequence_number billing_id ...
13 LJK1 1 4356
14 CNS3 2 4356
15 LJK1 3 4356
16 AOX4 4 4356
17 CNS3 1 9827
18 POD2 2 9827
It gets tricky. I need to return all the type_id pairs from one period to the next period based on incrementing sequence numbers. For example, for billing_id=’4356′, I need to retrieve this resultset
-----------------------------------
LJK1 | CNS3 -- type_ids pair from seq 1 to seq 2
CNS3 | LJK1 -- type_ids pair from seq 2 to seq 3
LJK1 | AOX4 -- type_ids pair from seq 3 to seq 4
The count of sequence_numbers per order is variable (as high as 10, as low as 2). I’ll be doing this for X number of orders. From this, I’ll be doing further processing, but getting these pairings itself is puzzling me. I don’t need order_id or sequence numbers in the resultset.
Are recursive CTEs needed here? Perhaps cursors? Or maybe using pivot? I’ve just been brainstorming and researching, but I’m not too experienced with any of those and could use some direction.
Using SQL Server 2008
I don’t think you need a recursive CTE nor cursors. Try this: