Created
July 10, 2022 22:58
-
-
Save Ninjanaut/411f9e2e7290363477a5cf75004c78c3 to your computer and use it in GitHub Desktop.
Fill empty values based on the value in previous rows
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| declare @temp as table ( | |
| DayDate date not null, | |
| Inventory int null | |
| ) | |
| insert into @temp | |
| values | |
| ('01-01-2022', 1234), | |
| ('02-01-2022', null), | |
| ('03-01-2022', null), | |
| ('04-01-2022', 2345), | |
| ('05-01-2022', null), | |
| ('06-01-2022', 3456), | |
| ('07-01-2022', null), | |
| ('08-01-2022', null) | |
| select * from @temp | |
| declare @previousValue int | |
| UPDATE @temp | |
| SET | |
| @previousValue = COALESCE(Inventory, @previousValue), | |
| Inventory = COALESCE(Inventory, @previousValue) | |
| select * from @temp |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment