The following thread (SQL QUERY replace NULL value in a row with a value from the previous known value) was very helpful to carry forward non-null values, but I'm can't figure out how to add a grouping column.
For example, I would like to do the following:
Here is how I would have liked to code it:
UPDATE t1
SET
@n = COALESCE(number, @n),
number = COALESCE(number, @n)
GROUP BY grp
I know this isn't possible, and have seen several solutions that rely on inner joins, but those examples focus on aggregation rather than carrying forward values. For example: SQL Server Update Group by.
My attempt to make this work is to do something like the following:
UPDATE t1
SET
@n = COALESCE(number, @n),
number = COALESCE(number, @n)
FROM t1
INNER JOIN (
-- Lost on what to put in the inner join...
SELECT grp, COUNT(*) FROM t1 GROUP BY grp
) t2
on t1.grp = t2.grp