Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7406751
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T05:36:19+00:00 2026-05-29T05:36:19+00:00

I am trying to write a SQL query that links call segments together, dynamicly

  • 0

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!

Calls

Table Structure:
CallSegment
– CallSegmentID

CallSegmentLink
– CallSegmentLinkID
– CallSegmentParentID
– CallSegmentChildID

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-29T05:36:19+00:00Added an answer on May 29, 2026 at 5:36 am

    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:

    -- Set up the test data.
    declare @CallSegments as table ( CallSegmentId int )
    insert into @CallSegments ( CallSegmentId ) values ( 1 ), ( 2 ), ( 3 ), ( 4 ), ( 5 ), ( 6 ), ( 7 ), ( 8 )
    
    declare @CallSegmentLinks as table ( ParentCallSegmentId int, ChildCallSegmentId int )
    insert into @CallSegmentLinks ( ParentCallSegmentId, ChildCallSegmentId ) values
      ( 1, 2 ), ( 2, 3 ), ( 3, 4 ),
      ( 6, 7 ), ( 6 , 8 )
    
    -- Find all of the   CallSegments   which are not targets of   CallSegmentLinks   and assign a   CallNumber   to each.
    declare @Calls as table ( InitialCallSegmentId bit, CallSegmentId int, CallNumber int )
    insert into @Calls
      select 1, CallSegmentId, Row_Number() over ( order by CallSegmentId ) as CallNumber
        from @CallSegments
        where not exists ( select 42 from @CallSegmentLinks where ChildCallSegmentId = CallSegmentId )
    
    -- Add the remaining call segments.
    insert into @Calls
      select 0, CallSegmentId, NULL
        from @CallSegments
        where CallSegmentId not in ( select CallSegmentId from @Calls )
    
    -- Propogate the   CallNumber   to the remaining call segments.
    while ( @@RowCount > 0 )
      begin
      update Child
        set CallNumber = Parent.CallNumber
        from @Calls as Parent inner join
          @CallSegmentLinks as CSL on CSL.ParentCallSegmentId = Parent.CallSegmentId inner join
          @Calls as Child on Child.CallSegmentId = CSL.ChildCallSegmentId
        where Child.CallNumber is NULL and Parent.CallNumber is not NULL
      end
    
    -- Display the grouped call segments with the initial call segment for each call shown first.
    select * from @Calls order by CallNumber, InitialCallSegmentId desc, CallSegmentId
    

    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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to write an SQL query that would search within a CSV (or
I am trying to write a SQL query that pulls from 3 tables and
I'm trying to write a custom SQL query that will create a list of
Im trying to write an SQL query that will check in the table ‘persons’
I'm trying to write a sql query that shows how often two teams have
I am trying to write a SQL query that returns rows from a table
I'm trying to learn SQL and trying to write a SQL Query that does
I am trying to write a SQL query that does some complex sorting. I
I am trying write a SQL query that filters a gridview by the fields
I'm trying to write an SQL query that will gather certain data in one

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.