Reputation: 47
I have input data set as below. I am trying to populate null values with last available value.
INPUT DATA SET :
EXPECTED DATA SET :
QUERY I TRIED :
SELECT *
,ISNULL(YEAR, (
SELECT TOP 1 YEAR FROM SEQ_SET
WHERE GROUPSEQ <= T.GROUPSEQ
AND YEAR IS NOT NULL
ORDER BY GROUPSEQ DESC)) AS YEAR_APPLY
,ISNULL(QUARTER, (
SELECT TOP 1 QUARTER FROM SEQ_SET
WHERE GROUPSEQ <= T.GROUPSEQ
AND QUARTER IS NOT NULL
ORDER BY GROUPSEQ DESC)) AS QUARTER_APPLY
FROM SEQ_SET T
ORDER BY COMPANY, YEAR, QUARTER
Whats going wrong :
All values are coming as expected except the one in groupseq 6. ( the value highlighted in yellow ). It should be Q1 instead of Q2.
i.e. when there are more than two nulls the logic does not work
any idea whats missing in my query.
Reference post :
How to get Previous Value for Null Values
Upvotes: 1
Views: 48
Reputation: 1271013
Basically, you seem to want lag(ignore nulls)
-- the most recent non-NULL
value. SQL Server does not support this standard functions.
One method instead is to assign groups by counting the number of non-NULL values up to each row and then use window functions:
select t.*,
max(year) over (partition by company, grp) as year_apply,
max(quarter) over (partition by company, grp) as quarter_apply
from (select t.*,
count(year) over (partition by company order by groupseq) as grp
from seq_set t
) t
Upvotes: 1