Click here to monitor SSC
SQLServerCentral is supported by Red Gate Software Ltd.
 
Log in  ::  Register  ::  Not logged in
 
 
 
        
Home       Members    Calendar    Who's On


Add to briefcase

Reporting in records in the right order Expand / Collapse
Author
Message
Posted Monday, April 29, 2013 7:21 AM
Old Hand

Old HandOld HandOld HandOld HandOld HandOld HandOld HandOld Hand

Group: General Forum Members
Last Login: Thursday, April 10, 2014 3:49 PM
Points: 366, Visits: 577
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



Post #1447503
Posted Monday, April 29, 2013 7:34 AM


SSChampion

SSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampion

Group: General Forum Members
Last Login: Today @ 1:54 PM
Points: 12,741, Visits: 31,051
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

--There is no spoon, and there's no default ORDER BY in sql server either.
Actually, Common Sense is so rare, it should be considered a Superpower. --my son
Post #1447512
Posted Monday, April 29, 2013 7:42 AM
Old Hand

Old HandOld HandOld HandOld HandOld HandOld HandOld HandOld Hand

Group: General Forum Members
Last Login: Thursday, April 10, 2014 3:49 PM
Points: 366, Visits: 577
@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
Post #1447523
Posted Monday, April 29, 2013 6:41 PM


SSC-Dedicated

SSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-Dedicated

Group: General Forum Members
Last Login: Today @ 9:48 AM
Points: 35,950, Visits: 30,232
[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." -- 04 August 2013
(play on words) "Just because you CAN do something in T-SQL, doesn't mean you SHOULDN'T." --22 Aug 2013

Helpful Links:
How to post code problems
How to post performance problems
Post #1447829
Posted Monday, April 29, 2013 6:53 PM


SSCommitted

SSCommittedSSCommittedSSCommittedSSCommittedSSCommittedSSCommittedSSCommittedSSCommitted

Group: General Forum Members
Last Login: Today @ 12:35 PM
Points: 1,651, Visits: 5,197
Will there always be a path that includes all rows for a particular Ref#?

MM


  • MMGrid Addin
  • MMNose Addin


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

  • Post #1447833
    « Prev Topic | Next Topic »

    Add to briefcase

    Permissions Expand / Collapse