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.

 All Forums
 General SQL Server Forums
 New to SQL Server Programming
 Remove Null from Pivot Data

Author  Topic 

GLupien
Starting Member

2 Posts

Posted - 2015-01-17 : 06:47:25
I have a pivot query returning data ordered by a timestamp. In any give second only a few of the columns will have data, the remaining columns are NULL. I would like to default the NULL data values to the previous NON NULL value by timestamp.

Current Pivot Table:

Time RowA RowB RowC
12:15:12 NULL NULL 50
12:15:11 97 NULL NULL
12:15:10 98 25 51
12:15:06 101 NULL 50
12:15:05 100 NULL NULL


What should be returned:

Time RowA RowB RowC
12:15:12 97 25 50
12:15:11 97 25 51
12:15:10 98 25 51
12:15:06 101 NULL 50
12:15:05 100 NULL NULL


I know that I can do this user a cursor and handling the output is code, but I would prefer to do this in a single set based query.

Regards,
Grant

Regards,
Grant F. Lupien

gbritton
Master Smack Fu Yak Hacker

2780 Posts

Posted - 2015-01-17 : 11:10:24
If you're running sql server 2012 or 2014, read up on the LAG function. You can use it to do just what you want.
Go to Top of Page
   

- Advertisement -