Background:Migrating a database that violates every normal form (even 1NF!) and transforming into a normalised database.SQL2K.....My problem is one of performance in "compressing" rows Here is stripped version...Removed Key definitions (They don't have any on the "old" table anyway) and added the Identity column into the New table for ease....--Old Tablecreate table Notes (Record_Number int not null, DN int not null, Number int, Note varchar(50))goinsert Notes (1,1,1,'Test1')insert Notes (2,1,2,'Test2')insert Notes (3,2,1,'Test1')insert Notes (4,2,2,'Test2')insert Notes (5,1,3,'Test3')insert Notes (6,2,3,'Test3')insert Notes (7,4,1,'Test1')go--New tablecreate table NewNotes(NoteID int identity(1,1) not null,DN int not null, Note varchar(50) not null)go
As you can see the "Note" has a Sequence (Record_Number), what should be a Foreign Key of DN and a Note Number...They would like consecutive Notes to be "merged" into a single note.The defintion of "consecutive" is Record_Number sequence and Number sequence.From the sample data Rows 1,2 are compressed into 1 rowRows 3,4 are compressed into 1 rowRow 5 to a single rowRow 6 to a single rowRow 7 to a single rowMy soution so far which has terrible performance.. (I am dealing in millions of rows here...)First a query to find these sequences...Select DN, Count(DN) as CNT , min(Record_Number) as StartRec from Notesgroup by DN, (Record_Number - Number)
This will generate the DN, the number of rows to compress and the "start" number of the row sequence.Now create a row compression function...create function fnCompressNote(@StartRec int,@Count int)returns varchar(1000)asbegindeclare @Note varchar(1000)Select @Note = @Note + Note + '| ' from Notes where Record_Number between @StartRec and (@StartRec + @Count -1)return @Noteendgo
So to tie it all together....Select DN, dbo.fnCompressNote(StartRec ,CNT) as Notefrom(Select DN, Count(DN) as CNT , min(Record_Number) as StartRec from Notesgroup by DN, (Record_Number - Number)) as X
It works.. but realy slowly....Any other solutions would be most wlecome....DavidM"SQL-3 is an abomination.."