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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 18, 20262026-05-18T20:34:27+00:00 2026-05-18T20:34:27+00:00

I have a table which contains chronological events. They have a type, time and

  • 0

I have a table which contains chronological events. They have a type, time and height.

The data can be summarized like so:

ID, Type, Time, Height  
1, A, time, 0  
2, XX, time, 0   
3, B, time, 3  
4, B, time, 6  
5, C, time, 0  
6, XX, time, 0  
7, A, time, 0  
8, C, time, 0  
9, A, time, 0  
10, B, time, 2  
11, C, time, 0  

etc ( the time column is sorted in ascending order)

i would like to find a SQL statement to list all types of A/B/C where B is the maximum of the height column between types A and C.
So the output would look like:

1, A, time, 0   
4, B, time, 6  
5, C, time, 0  
7, A, time, 0  
8, C, time, 0  
9, A, time, 0  
10, B, time, 2  
11, C, time, 0  

The A/B and C will always be in correct order (i.e. B will always be between A and C), But there may not be a B at all, or there may be multiple B’s between A and C.

The output may/may not list a B event with NULL data if there is no B between A and C.
There is guaranteed to be a C after every A type event.

All XX events shall be ignored in the output. Timestamps over the list will never be duplicated – no two events will contain the same time.

Im guessing to use the MAX function somewhere, and to select all B Rows between A and C depending on the time of A and C.

TIA

  • 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-18T20:34:28+00:00Added an answer on May 18, 2026 at 8:34 pm

    Not sure if I have this 100% right, but I find that it’s always best to break this stuff down into smaller queries into temp tables. Here’s a crack at it… (BTW – this is SQL Server T-SQL)

    -- get all the type 'a' and type 'c' IDs to represent time spans
    if object_id('tempdb..#tab_ac') is not null drop table #tab_ac
    select
        a.ID as A_ID,
        (
            select top 1 c.ID
            from tab c
            where c.Time > a.Time
            and c.Type = 'C'
            order by c.Time
        ) as C_ID
    into
        #tab_ac
    from
        tab a
    where
        a.Type = 'A'
    
    create index ix_#tab_ac on #tab_ac (A_ID, C_ID)    
    
    -- get the id with the max height between those spans
    if object_id('tempdb..#result1') is not null drop table #result1
    select
        ac.*,
        (
            select x.ID
            from tab x
            where x.Time between ta.Time and tc.Time
            order by a.Height desc
        ) as ID_With_Max_Height
    into
        #result1
    from
        #tab_ac ac join
        tab ta on ac.A_ID = t.ID join
        tab tc on ac.C_ID = t.ID
    
    -- see if that id is type 'B'
    select
        *
    from
        #result1 r join
        tab t on r.ID_With_Max_Height = t.ID
    where
        t.Type = 'B'
    

    Depending on how you want to handle ties for max height, you may want to modify that second query’s ORDER BY clause. Good luck.

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

Sidebar

Related Questions

I have a table which contains chronological events. They have a type, time and
I have a table which contains two type of data, either for Company or
I have a table which contains my ads that can be searched in sql-server-2008.
I have a table which contains data about which node has been visited. It
I have table which contains 2 date fields. Like: id from to --- ----
I have a table which contains data in different languages. All fields are nvarchar(max).
I have a table which contains null values and I need to get data
I have a table which contains the following data: ID In Out 1 100.00
I have a table which contains sales order data (order number, product number, sales
I have table which contains data along with created on date. I want to

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.