Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save aramkoukia/e6b844493f59de0d9182b24be9cff124 to your computer and use it in GitHub Desktop.
Save aramkoukia/e6b844493f59de0d9182b24be9cff124 to your computer and use it in GitHub Desktop.
Filling NULL values with Preceding Non-NULL values - Option 2
-- First of all I'll just put the data from my main table into a temp table
-- just to make sure i am not destroying the actual data if something went wrong.
SELECT * INTO #Temp FROM ImportedSales;
;With CTE As
(
SELECT ProductName , Id , COUNT(ProductName)
OVER(ORDER BY Id ROWS UNBOUNDED PRECEDING) As MyGroup FROM #Temp ), GetProduct AS (
SELECT [ProductName] , First_Value(ProductName)
OVER(PARTITION BY MyGroup
ORDER BY Id ROWS UNBOUNDED PRECEDING
) As UpdatedProductName FROM CTE
)
UPDATE GetProduct
SET ProductName = UpdatedProductName;
SELECT * FROM #Temp;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment