I am trying to write a SQL query that links call segments together, dynamicly creating a unique ‘CallID’ column. There are two tables in the database that are used for this, the Call Segment table and the Call Segment Link table which is a One to Many linking table. One call segment can link to many other call segments. Not all call segments have a record in the call segment link table. A call segment link only gets created if the call is transfered.
I am using SQL Server 2008 R2. My ultimate goal is to end up with a query that outputs one row for every Call Segment with one additional column Call ID (the ID of the first call segment).
If anyone has any idea how this would be possible with SQL, I would be very greatful for the help.
Thanks!

Table Structure:
CallSegment
– CallSegmentID
CallSegmentLink
– CallSegmentLinkID
– CallSegmentParentID
– CallSegmentChildID
While sequential call segments admit to processing by CTE, the parallel call segments threw in quite a stumbling block.
The following code will assign call numbers to each call and group the call segments appropriately:
Note that the first call segment within each call is flagged. This may help you write additional code that chases through the segments within a call in an order or your own devising.