April 29, 2013 at 7:21 am
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
April 29, 2013 at 7:34 am
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
April 29, 2013 at 7:42 am
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
April 29, 2013 at 6:41 pm
[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
Change is inevitable... Change for the better is not.
April 29, 2013 at 6:53 pm
Will there always be a path that includes all rows for a particular Ref#?
MM
select geometry::STGeomFromWKB(0x0106000000020000000103000000010000000B0000001000000000000840000000000000003DD8CCCCCCCCCC0840000000000000003DD8CCCCCCCCCC08408014AE47E17AFC3F040000000000104000CDCCCCCCCCEC3F9C999999999913408014AE47E17AFC3F9C99999999991340000000000000003D0000000000001440000000000000003D000000000000144000000000000000400400000000001040000000000000F03F100000000000084000000000000000401000000000000840000000000000003D0103000000010000000B000000000000000000143D000000000000003D009E99999999B93F000000000000003D009E99999999B93F8014AE47E17AFC3F400000000000F03F00CDCCCCCCCCEC3FA06666666666FE3F8014AE47E17AFC3FA06666666666FE3F000000000000003D1800000000000040000000000000003D18000000000000400000000000000040400000000000F03F000000000000F03F000000000000143D0000000000000040000000000000143D000000000000003D, 0);
Viewing 5 posts - 1 through 4 (of 4 total)
You must be logged in to reply to this topic. Login to reply