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 4122562
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T23:29:51+00:00 2026-05-20T23:29:51+00:00

I have a table (time_period) The table lists several time periods like so. unique_id

  • 0

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

  • 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-20T23:29:52+00:00Added an answer on May 20, 2026 at 11:29 pm

    I don’t think you need a recursive CTE nor cursors. Try this:

        SELECT A.type_id, B.type_id
        FROM time_period A
        LEFT JOIN time_period B
        ON A.billing_id = B.billing_id
        AND A.sequence_number = B.sequence_number - 1
        WHERE A.billing_id = 4356
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a table containing reports and the date/time they were created. I'd like
I have table with date/time column and person column. Each person has multiple records
I have a table of time-series data of which I need to find all
I have the following table of TIME datatypes clarification: I am representing hours/mins/seconds of
I have a table populated with time stamped rows inserted at (essentially) random point
I have table that contain date and time field. id|date|time ========= 1|01/01/2001|10:45 2|01/02/2002|11:45 3|01/03/2003|12:45
I have table with a unique auto-incremental primary key. Over time, entries may be
I have a table which will have a date/time column to store create date
i have a table where i want to track time so a valid entry
Say I have a summary table like this: AcitivityCache ( pkId (bigint, PK), Date

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.