I am trying to update NULL column values with values from the same column that are from the same Item and DateAdded:
Before:
DateAdded | Item | SalePrice | SaleDate |
---|---|---|---|
1/02/2024 | Apple | 99 | 1/12/2024 |
1/02/2024 | Apple | NULL | NULL |
2/05/2024 | Apple | 102 | 2/12/2024 |
2/05/2024 | Apple | NULL | NULL |
2/05/2024 | Banana | NULL | NULL |
2/05/2024 | Banana | 101 | 2/13/2024 |
2/05/2024 | Banana | NULL | NULL |
2/06/2024 | Banana | NULL | NULL |
After:
DateAdded | Item | SalePrice | SaleDate |
---|---|---|---|
1/02/2024 | Apple | 99 | 1/12/2024 |
1/02/2024 | Apple | 99 | 1/12/2024 |
2/05/2024 | Apple | 102 | 2/12/2024 |
2/05/2024 | Apple | 102 | 2/12/2024 |
2/05/2024 | Banana | 101 | 2/13/2024 |
2/05/2024 | Banana | 101 | 2/13/2024 |
2/05/2024 | Banana | 101 | 2/13/2024 |
2/06/2024 | Banana | NULL | NULL |
I get the following error with the below code:
Update table1
SET SaleDate = ISNULL(SaleDate, coalesce(SaleDate, max(SaleDate) over (partition by Item, CAST([DateAdded] AS DATE)))
,SalePrice = ISNULL(SalePrice,coalesce(SalePrice, max(SalePrice) over (partition by Item, CAST([DateAdded] AS DATE)))
WHERE CAST(DateAdded AS DATE) > '2024-01-01'
Windowed functions can only appear in the SELECT or ORDER BY clauses.