I have the following query:
SELECT * FROM ( SELECT unix_timestamp, input_raw, tag_id from [200030].[dbo].inputs WHERE inputs.date_time > dateadd(day,-1,getdate()) AND (tag_id = 92164 or tag_id = 92149) ) src pivot ( max(input_raw) FOR tag_id IN ([92164], [92149]) ) piv ORDER by unix_timestamp DESC
which is great and works. This gives me the results:

However, I would like the request to do one more thing for me.
Whenever there is a “NULL” result, I would like the query to replace “NULL” with the last “non-NULL” value in the column.
For example, the first NULL specified in column "92164" will be replaced with "211".
In addition, it is possible that there will be a few “NULL I row”, so the query will have to keep going up the column until it finds NULL.
I was able to accomplish this with php. Putting the results in a 2D associative array and running a function that finds zeros, then iterates over to find the last one is not NULL, but I really want to do all this in SQL, if possible. I would rather use
while( $row = sqlsrv_fetch_array( $stmt, SQLSRV_FETCH_ASSOC)){
than assigning multiple arrays.
Any help?
thanks
// EDIT
I forgot to add that this is applicable only if there are non-zero values that exceed the zero value. For example, it is valid if the first line is NULL.
Mildfire
source share