Author |
Topic |
DLTaylor
Posting Yak Master
136 Posts |
Posted - 2007-10-03 : 12:09:54
|
I'm new to T-SQL programming and was hoping for some help.I have a table SQL 2000 Table that contains data looking like this;PK,Spec,Date,Time1,AE,20/05/2007,9002,,21/05/2007,9003,,22/05/2007,900 4,ORTH,20/05/2007,8555,,21/05/2007,8556,OPTH,18/06/2007,9057,,19/06/2007,910I would like the build a statement where the Spec column is updated to reflect the value of the Spec. So as it looks like this...PK,Spec,Date,Time1,AE,20/05/2007,9002,AE,21/05/2007,9003,AE,22/05/2007,900 4,ORTH,20/05/2007,8555,ORTH,21/05/2007,8556,ENT,18/06/2007,9057,ENT,19/06/2007,910Is there a way to achieve this.Any help would be most appreciated.Thanks |
|
SwePeso
Patron Saint of Lost Yaks
30421 Posts |
Posted - 2007-10-03 : 12:14:10
|
UPDATE t1SET t1.Spec = (SELECT TOP 1 t2.Spec FROM Table1 AS t2 WHERE t2.Spec IS NOT NULL AND t2.PK < t1.PK ORDER BY t2.PK DESC)FROM Table1 AS t1WHERE t1.Spec IS NULL E 12°55'05.25"N 56°04'39.16" |
 |
|
sshelper
Posting Yak Master
216 Posts |
Posted - 2007-10-03 : 12:15:03
|
Using the Time column as the common value for the Spec, you can try the following UPDATE statement:UPDATE ASET [Spec] = B.[Spec]FROM YourTable A INNER JOIN YourTable BON A.[Time] = B.[Time] AND A.[Spec] = '' AND B.[Spec] != ''SQL Server Helperhttp://www.sql-server-helper.com |
 |
|
SwePeso
Patron Saint of Lost Yaks
30421 Posts |
Posted - 2007-10-03 : 12:17:57
|
Won't work for line 6 & 7.-- Prepare sample dataDECLARE @Sample TABLE (PK INT, Spec VARCHAR(4), Date DATETIME, Time INT)SET DATEFORMAT DMYINSERT @SampleSELECT 1, 'AE', '20/05/2007', 900 UNION ALLSELECT 2, NULL, '21/05/2007', 900 UNION ALLSELECT 3, NULL, '22/05/2007', 900 UNION ALLSELECT 4, 'ORTH', '20/05/2007', 855 UNION ALLSELECT 5, NULL, '21/05/2007', 855 UNION ALLSELECT 6, 'OPTH', '18/06/2007', 905 UNION ALLSELECT 7, NULL, '19/06/2007', 910-- Do the updateUPDATE t1SET t1.Spec = (SELECT TOP 1 t2.Spec FROM @Sample AS t2 WHERE t2.Spec IS NOT NULL AND t2.PK < t1.PK ORDER BY t2.PK DESC)FROM @Sample AS t1WHERE t1.Spec IS NULL-- Show the expected outputSELECT * FROM @Sample E 12°55'05.25"N 56°04'39.16" |
 |
|
DLTaylor
Posting Yak Master
136 Posts |
Posted - 2007-10-03 : 12:52:55
|
 Worked exactly as I’d wished.Thanks for all your help - esp. PESO! |
 |
|
|
|
|