Need to find gaps in a sequence which increments by 2

  • Hi Friends,

    I'm trying to write a query to validate the data.

    Here is the scenario:

    1. The table has Three columns 1.ID, 2.Sqno, 3. Adj

    2. The values for adj are (0,1,2)

    Case1: The Sqno should start at '001000' for adj in (0,2) and increment by 2, i.e the next sqno would be '001002' and '001004' so on.

    Case2: The sqno should start at '001001' for adj in (1) and increment by 2 i.e the next sqno would be '001003' and '001005' so on.

    Finally when you do order by sqno and group by ID it will be a running sqno.

    ID Sqno Adj

    123A 001000 0

    123A 001001 1

    123A 001002 2

    123A 001003 1

    123A 001004 2

    123A 001005 1

    123A 001006 0

    123A 001007 1

    123A 001008 2

    123A 001009 1

    .......

    Can you please help me write a query that can validate this scenario.

  • ezapa001 (9/10/2014)


    Hi Friends,

    I'm trying to write a query to validate the data.

    Here is the scenario:

    1. The table has Three columns 1.ID, 2.Sqno, 3. Adj

    2. The values for adj are (0,1,2)

    Case1: The Sqno should start at '001000' for adj in (0,2) and increment by 2, i.e the next sqno would be '001002' and '001004' so on.

    Case2: The sqno should start at '001001' for adj in (1) and increment by 2 i.e the next sqno would be '001003' and '001005' so on.

    Finally when you do order by sqno and group by ID it will be a running sqno.

    ID Sqno Adj

    123A 001000 0

    123A 001001 1

    123A 001002 2

    123A 001003 1

    123A 001004 2

    123A 001005 1

    123A 001006 0

    123A 001007 1

    123A 001008 2

    123A 001009 1

    .......

    Can you please help me write a query that can validate this scenario.

    Your question is rather unclear. Are you using 2 different sequences for the same table? What is the rationale for that? It also appears from your data that you are using varchar to hold these values? What happens when you exceed 500 rows for one of the sequences?

    It should be reasonably easy to find gaps. You just need to use modulus to know "which" sequence you are looking at.

    _______________________________________________________________

    Need help? Help us help you.

    Read the article at http://www.sqlservercentral.com/articles/Best+Practices/61537/ for best practices on asking questions.

    Need to split a string? Try Jeff Modens splitter http://www.sqlservercentral.com/articles/Tally+Table/72993/.

    Cross Tabs and Pivots, Part 1 – Converting Rows to Columns - http://www.sqlservercentral.com/articles/T-SQL/63681/
    Cross Tabs and Pivots, Part 2 - Dynamic Cross Tabs - http://www.sqlservercentral.com/articles/Crosstab/65048/
    Understanding and Using APPLY (Part 1) - http://www.sqlservercentral.com/articles/APPLY/69953/
    Understanding and Using APPLY (Part 2) - http://www.sqlservercentral.com/articles/APPLY/69954/

Viewing 2 posts - 1 through 2 (of 2 total)

You must be logged in to reply to this topic. Login to reply