Question : Query with value determined by value on previous row

The following query returns nulls for QtyOnHand when there is no value for that date in #TempQtyOnHand6.

Instead, I would like to set a counter or something so that on 1/1/2005 the value returned (QtyOnHand)would be zero, and the value returned (QtyOnHand) would continue to be 0 for every subsequent row UNTIL the first date that matches in #TempQtyOnHand6.  For that date it should bring the qty from #TempQtyOnHand6 (which it already does), and then for each subsequent row the QtyOnHand should be the same as the previous row UNTIL it finds the next matching date.

See attached for sample of the results I want to see
Code Snippet:
1:
2:
3:
4:
5:
6:
7:
8:
select DateFull, QtyOnHand
 
from dbo.tsi_view_DateLookup LEFT OUTER JOIN #TempQtyOnHand6
ON dbo.tsi_view_DateLookup.DateFull = #TempQtyOnHand6.trans_date
 
where (WeekDayName not like 's%') AND (DateFull between '01/01/2005 00:00:00' and '04/30/2008 23:59:59')
 
ORDER BY DateFull
Open in New Window Select All

Answer : Query with value determined by value on previous row

Can you try this:

SELECT DateFull, ISNULL(QtyOnHand, 0)
FROM dbo.tsi_view_DateLookup
LEFT OUTER JOIN #TempQtyOnHand6 ON
   #TempQtyOnHand6.trans_date = (SELECT MAX(trans_date) FROM #TempQtyOnHand6 WHERE trans_date <= DateFull)
WHERE (WeekDayName NOT LIKE 's%') AND (DateFull BETWEEN '01/01/2005 00:00:00' AND '04/30/2008 23:59:59')
ORDER BY DateFull
Random Solutions  
 
programming4us programming4us