Please start any new threads on our new
site at https://forums.sqlteam.com. We've got lots of great SQL Server
experts to answer whatever question you can come up with.
Author |
Topic |
Liza-Jane
Starting Member
3 Posts |
Posted - 2013-09-02 : 05:44:09
|
HiI've got a series of statements to run on a single table which should amalgamate one of the fields onto one of the records - and in the main it does. Except when it gets to the last two data fields to be joined. There are 3 stages - 1. update the max_ptr; 2. concatenate the data; 3. delete the line.I repeat these 3 until the last one returns 0 deletions.The data in the table looks like this:Lic Code TextData max_ptr min_ptr IDLIC-5717 L08 Mon-Thu-23:00-00:00 NULL 322530 322530LIC-5717 L08 Fri-Sat-23:00-02:00 322531 322530 322531LIC-5816 M08 Mon-Sat-23:00-00:00 NULL 322526 322526LIC-5816 M08 Sun-23:00-23:30 322527 322526 322527LIC-5816 M08 Good Fri-23:00-23:30 322527 322526 322534LIC-5816 M08 Christmas-23:00-23:30 322527 322526 322535And these are the statements:1. update TABLE set max_ptr = (select min(ID) from TABLE t2 where TABLE.Lic = t2.Lic and TABLE.Code = t2.Code and TABLE.ID != min_ptr and t2.ID != min_ptr group by Lic, Code) 2. update TABLE set TextData = TextData + '|' + (select t2.TextData from TABLE t2 where t2 ID = t2.max_ptr and t2.Lic = TABLE.Lic and t2.Code = TABLE.Code) where ID = min_ptr and Lic+Code in (select Lic+Code from TABLE t3 where t3.ID != t3.max_ptr) 3.delete from TABLE where ID = max_ptr and ID != min_ptr So, after running the 3 through once I get the following in the table:Lic Code TextData max_ptr min_ptr IDLIC-5717 L08 Mon-Thu-23:00-00:00 NULL 322530 322530LIC-5816 M08 Mon-Sat-23:00-00:00|Sun-23:0 NULL 322526 322526LIC-5816 M08 Good Fri-23:00-23:30 322527 322526 322534LIC-5816 M08 Christmas-23:00-23:30 322527 322526 322535 It has deleted the 2nd line for LIC-5717 as expected but hasn't added the TextData to the first entry.Looking at the code for statement 2 I can see that the last part of the statement is where the problem lies but I'm at a loss on how to put it right.I've been left various scripts to run and I thought they were tried and tested as they've been used before. I can't contact the writer and my other goto person is on holiday.Can anyone suggest a method of adapting the statement so that I can get on with my life? I'd be very grateful.thanks. |
|
Liza-Jane
Starting Member
3 Posts |
Posted - 2013-09-02 : 10:10:52
|
Since adding this question I've been trying all sorts of different methods to get what I want. The nearest I've come is using some code I found on another forum which nearly does what I want. I'm so close ... and yet so far. I tried this:select Lic, Code, min_ptr,(select cast(Textdata + '|' as varchar(max))from TABLEwhere (min_ptr = TABLE.min_ptrand Lic+Code = TABLE.Lic+Codefor XML path('')) as TextResultsfrom TABLEwhere min_ptr = TABLE.min_ptrgroup by LIC, min_ptr, code I get back the rows I'm expecting but each of them have all of the textdata information on - not just the data relevant to their own ids.What am I doing wrong? |
 |
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2013-09-02 : 10:21:51
|
just a small modificationselect Lic, Code, min_ptr,(select cast(Textdata + '|' as varchar(max))from TABLEwhere min_ptr = t.min_ptrand Lic+Code = t.Lic+Codefor XML path('')) as TextResultsfrom (SELECT DISTINCT Lic, Code, min_ptr FROM TABLE)twhere min_ptr = TABLE.min_ptrgroup by LIC, min_ptr, code ------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/https://www.facebook.com/VmBlogs |
 |
|
Liza-Jane
Starting Member
3 Posts |
Posted - 2013-09-02 : 10:26:38
|
Whoo! I think I've cracked it! I adjusted the last code I used to add table definers (if that's the correct term) and I removed the last 'WHERE' and hey presto! I got the lines I required with the data I required.select Lic, Code, min_ptr,(select cast(Textdata + '|' as varchar(max))from TABLE t1where (t1.min_ptr = t2.min_ptrand t1.Lic+Code = t2.Lic+Codefor XML path('')) as TextResultsfrom TABLE t2group by LIC, min_ptr, codethanks v much!  |
 |
|
|
|
|
|
|