Reporting in records in the right order

  • hi guys

    I have some data from a system upgrade where stuff didn't get the right sequence numbers

    create #test

    (

    ref int,

    oldvalue nvarchar,

    newvalue nvarchar,

    seqno int

    )

    insert into #test (ref,oldvalue,newvalue,seqno) values (100,null,'A',1)

    insert into #test (ref,oldvalue,newvalue,seqno) values (100,'B','C',2)

    insert into #test (ref,oldvalue,newvalue,seqno) values (100,'A','B',3)

    insert into #test (ref,oldvalue,newvalue,seqno) values (100,'B','X',4)

    insert into #test (ref,oldvalue,newvalue,seqno) values (100,'C','B',5)

    the correct order for these records is actually 1,3,2,5,4 (oldvalue = newvalue from previous record). The challenge is that from seqno 3 you have two potential choices (2 or 4) but only the combination above collects all of the records.

    I can't see how to do this without resorting to cursors or other RBAR solutions. any help would be appreciated. The dataset is approximately 900K records spread over 250K ref#s

  • aaron i'm sure you know that SQL doesn't order data in any specific order without an explicit ORDER BY clause.

    in your case, i'm thinking you might do what you want with a CASE statement, but i'm a little unclear on how the records actually match;

    here's my first guess:

    SELECT t1.*

    FROM #test t1

    LEFT OUTER JOIN #test t2

    ON t1.ref = t2.ref

    and t1.oldvalue = t2.newvalue

    ORDER BY

    t1.ref,

    CASE WHEN t1.oldvalue is NULL THEN 1

    WHEN t1.oldvalue = t2.newvalue then 2

    else 3

    end,

    seqno

    Lowell


    --help us help you! If you post a question, make sure you include a CREATE TABLE... statement and INSERT INTO... statement into that table to give the volunteers here representative data. with your description of the problem, we can provide a tested, verifiable solution to your question! asking the question the right way gets you a tested answer the fastest way possible!

  • @lowell,

    Thats the challenge, when you use LEFT OUTER JOIN, seq#3 will join to both seq#2 and seq#4

    In theory you have to traverse the entire tree to find out whether you can include all records. in a particular path. Short of using cursors to move records from a processed to an unprocessed state I can't see a simple method that will scale. It's not so much the SQL that I am struggling with as the actual programming concept. If I could get the stragegy I can probably write the SQL

  • [Edit] Sorry, misread the original problem so withdrew my comment. Not yet sure with how to resolve the "fork in the road". Reminds me of "Yogi". "When you come to a fork in the road, take it!". 😛

    --Jeff Moden


    RBAR is pronounced "ree-bar" and is a "Modenism" for Row-By-Agonizing-Row.
    First step towards the paradigm shift of writing Set Based code:
    ________Stop thinking about what you want to do to a ROW... think, instead, of what you want to do to a COLUMN.

    Change is inevitable... Change for the better is not.


    Helpful Links:
    How to post code problems
    How to Post Performance Problems
    Create a Tally Function (fnTally)

  • Will there always be a path that includes all rows for a particular Ref#?

    MM



    select geometry::STGeomFromWKB(0x0106000000020000000103000000010000000B0000001000000000000840000000000000003DD8CCCCCCCCCC0840000000000000003DD8CCCCCCCCCC08408014AE47E17AFC3F040000000000104000CDCCCCCCCCEC3F9C999999999913408014AE47E17AFC3F9C99999999991340000000000000003D0000000000001440000000000000003D000000000000144000000000000000400400000000001040000000000000F03F100000000000084000000000000000401000000000000840000000000000003D0103000000010000000B000000000000000000143D000000000000003D009E99999999B93F000000000000003D009E99999999B93F8014AE47E17AFC3F400000000000F03F00CDCCCCCCCCEC3FA06666666666FE3F8014AE47E17AFC3FA06666666666FE3F000000000000003D1800000000000040000000000000003D18000000000000400000000000000040400000000000F03F000000000000F03F000000000000143D0000000000000040000000000000143D000000000000003D, 0);

  • Forum Etiquette: How to post Reporting Services problems
  • [/url]
  • Forum Etiquette: How to post data/code on a forum to get the best help - by Jeff Moden
  • [/url]
  • How to Post Performance Problems - by Gail Shaw
  • [/url]

Viewing 5 posts - 1 through 4 (of 4 total)

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