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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T05:12:14+00:00 2026-06-15T05:12:14+00:00

I’m trying to write a query that follows this logic: Find the first following

  • 0

I’m trying to write a query that follows this logic:

Find the first following status code of an account that had a previous status code of X.

So if I have a table of:

id    account_num    status_code
64        1               X
82        1               Y
72        2               Y
87        1               Z
91        2               X
103       2               Z

The results would be:

id   account_num     status_code
82        1               Y
103       2               Z

I’ve come up with a couple of solutions but I’m not all that great with SQL and so they’ve been pretty inelegeant thus far. I was hoping that someone here might be able to point me in the right direction.

View:

SELECT account_number, id
FROM   table
WHERE  status_code = 'X'

Query:

SELECT account_number, min(id)
FROM   table
INNER JOIN view
ON table.account_number = view.account_number
WHERE table.id > view.id

At this point I have the id that I need but I’d have to write ANOTHER query that uses the id tolook up the status_code.

Edit: To add some context, I’m trying to find calls that have a status_code of X. If a call has a status_code of X we want to dial it a different way the next time we make an attempt. The aim of this query is to provide a report that will show the results of the second dial if the first dial resulted an X status code.

  • 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-06-15T05:12:16+00:00Added an answer on June 15, 2026 at 5:12 am

    Here’s a SQL Server solution.

    UPDATE

    The idea is to avoid a number of NESTED LOOP joins as proposed by Olaf because they roughly have O(N * M) complexity and thus extremely bad for your performance. MERGED JOINS complexity is O(NLog(N) + MLog(M)) which is much better for real world scenarios.

    The query below works as follows:

    RankedCTE is a subquery that assigns a row number to each id partioned by account and sorted by id which represents the time. So for the data below the output of this

    SELECT 
        id, 
        account_num, 
        status_code,
        ROW_NUMBER() OVER (PARTITION BY account_num ORDER BY id DESC) AS item_rank
    FROM dbo.Test
    

    would be:

    id          account_num status_code item_rank
    ----------- ----------- ----------- ----------
    87          1           Z           1
    82          1           Y           2
    64          1           X           3
    103         2           Z           1
    91          2           X           2
    72          2           Y           3
    

    Once we have them numbered we join the result on itself like this:

    WITH RankedCTE AS
    (
        SELECT 
            id, 
            account_num, 
            status_code,
            ROW_NUMBER() OVER (PARTITION BY account_num ORDER BY id DESC) AS item_rank
        FROM dbo.Test
    )
    SELECT 
        *
    FROM
        RankedCTE A
        INNER JOIN RankedCTE B ON 
                A.account_num = B.account_num
                AND A.item_rank = B.item_rank - 1    
    

    which will give us an event and a preceding event in the same table

    id          account_num status_code item_rank   id          account_num status_code item_rank
    ----------- ----------- ----------- ----------- ----------- ----------- ----------- -----------
    87          1           Z           1           82          1           Y           2
    82          1           Y           2           64          1           X           3
    103         2           Z           1           91          2           X           2
    91          2           X           2           72          2           Y           3
    

    Finally, we just have to take the preceding event with code “X” and the event with code not “X”:

        WITH RankedCTE AS
        (
            SELECT 
                id, 
                account_num, 
                status_code,
                ROW_NUMBER() OVER (PARTITION BY account_num ORDER BY id DESC) AS item_rank
            FROM dbo.Test
        )
        SELECT 
            A.id, 
            A.account_num, 
            A.status_code
        FROM 
            RankedCTE A
            INNER JOIN RankedCTE B ON 
                A.account_num = B.account_num
                AND A.item_rank = B.item_rank - 1
                AND A.status_code <> 'X'
                AND B.status_code = 'X'
    

    Query plans for this query and @Olaf Dietsche solution (one of the versions) are below.

    query plans

    Data setup script

    CREATE TABLE dbo.Test
    (
        id int not null PRIMARY KEY,
        account_num int not null,
        status_code nchar(1)
    )
    GO
    
    INSERT dbo.Test (id, account_num, status_code)
    SELECT 64 ,       1,               'X' UNION ALL
    SELECT 82 ,       1,               'Y' UNION ALL
    SELECT 72 ,       2,               'Y' UNION ALL
    SELECT 87 ,       1,               'Z' UNION ALL
    SELECT 91 ,       2,               'X' UNION ALL
    SELECT 103,       2,               'Z'
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I have this code to decode numeric html entities to the UTF8 equivalent character.
I have this code: - (void)parser:(NSXMLParser *)parser foundCDATA:(NSData *)CDATABlock { NSString *someString = [[NSString
I know there's a lot of other questions out there that deal with this
I'm trying to convert HTML to plain text. I get many &\#8217; &\#8220; etc.
I'm trying to create an if statement in PHP that prevents a single post
I ran into a problem. Wrote the following code snippet: teksti = teksti.Trim() teksti
I have a .ini file as follows: [playlist] numberofentries=2 File1=http://87.230.82.17:80 Title1=(#1 - 365/1400) Example
I am trying to understand how to use SyndicationItem to display feed which is

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.