Site Sponsored By: SQLDSC - SQL Server Desired State Configuration
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.
Hi, I'm not to sure if this is possible.I want to replicate the top row value. Below in the example the second row Description field's value is ''How can i replicate the top row value where there is blank within the same process.Process Description1 Test1I have the same problem where there is a different value, the db data is a bit messy. Regards
DECLARE @T TABLE (ID INT, VAL VARCHAR(10))INSERT INTO @T SELECT 1, 'A'UNION ALL SELECT 1, ''UNION ALL SELECT 2, 'B'UNION ALL SELECT 2, ''SELECT *, CASE WHEN VAL = '' THEN ( SELECT MAX(VAL) FROM @T WHERE ID = T.ID) ELSE VAL END AS 'NewVAL'FROM @T T
"There is only one difference between a dream and an aim.A dream requires soundless sleep to see,whereas an aim requires sleepless efforts to achieve..!!"
SwePeso
Patron Saint of Lost Yaks
30421 Posts
Posted - 2008-12-04 : 07:49:57
Then I guess this will work to (but not so efficient)
SELECT ID, VAL, MAX(VAL) OVER (PARTITION BY ID) AS NewValFROM @T
E 12°55'05.63"N 56°04'39.26"
ismailc
Constraint Violating Yak Guru
290 Posts
Posted - 2008-12-04 : 08:56:27
Thank you all and apologies for the question - Under pressureIt's not really efficient,I done the following: from dbo.fcEventHist EHistTaskjoin (select Event.processid as EventProcess, Event.CreateDate as EventCreateDate, Event.CreateId as EventCreateID from dbo.fcEventHist Event join (select max(EHist.id) as MaxEvId, processid as MaxEvProcess from dbo.fcEventHist EHist where EHist.EventID in ('95','123') group by processid) as MaxEv on MaxEv.MaxEvId = Event.ID and MaxEv.MaxEvProcess = Event.processid) as MaxEventon EHistTask.ProcessID = MaxEvent.EventProcessRegards
SwePeso
Patron Saint of Lost Yaks
30421 Posts
Posted - 2008-12-04 : 09:25:45
Oh, you want last record of each group?Try the new ROW_NUMBER() function.
SELECT *FROM ( SELECT *, ROW_NUMBER() OVER (PARTITION BY ProcessID ORDER BY ID DESC) AS RecID FROM dbo.fcEventHist ) AS dWHERE RecID = 1