This feels like a bug to me...
As experts answered, this is not a bug, just because The implementation of FIRST_VALUE and LAST_VALUE does not support IGNORE NULLs mode in SQLServer 2019.
When your data_table has intermixed NULL values, here are some alternative methods to achieve the same effect as IGNORE NULLS.
- You could filter out NULLs in a subquery and LAST_VALUE will return the last non-null value like we want. Then join that result set back to the original data table.Like this: SELECT d.event,d2.LastNotNullValue
FROM [AdventureWorks2019].[dbo].[DatabaseLog] d
INNER JOIN ( SELECT DISTINCT [event], FIRST_VALUE(posttime) OVER (PARTITION BY [event] ORDER BY posttime) as LastNotNullValue
FROM [AdventureWorks2019].[dbo].[DatabaseLog]
WHERE posttime IS NOT NULL
) d2
ON d.[event] = d2.[event]
2)Apply COUNT over partition over current row to end of the group bottom will be able to identify the last non null value within the group.
SELECT event
,MAX(CASE WHEN ValuesAhead = 1 THEN Posttime END) OVER (PARTITION BY Event) AS Last
FROM
(
SELECT *,
LAST_VALUE(OutputValue) OVER (PARTITION BY Event ORDER BY Posttime ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING) AS LastValue,
COUNT(CASE WHEN OutputValue IS NOT NULL THEN OutputValue END) OVER (PARTITION BY Event ORDER BY Posttime ROWS BETWEEN CURRENT ROW AND UNBOUNDED FOLLOWING) AS ValuesAhead
FROM[AdventureWorks2019].[dbo].[DatabaseLog]
)t
Refer to this article for more details: Simulating IGNORE NULLs Functionality On FIRST_VALUE, LAST_VALUE Functions
Best regards,
LiHong
If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".
Note: Please follow the steps in our Documentation to enable e-mail notifications if you want to receive the related email notification for this thread.
email-notifications