سؤال

Here is my table:

ID  Quantity    Item

1   23  10
2   45  10
3   45  10
4   23  10
5   87  10
6   100 NULL

Here is my query:

SELECT ID, case lead(Item) over (order by ID) when null then 1
else 0 end as Test
FROM tblA

Here is my output:

ID  Test

1   0
2   0
3   0
4   0
5   0
6   0

Why is the last row of output is 6 and 0 and not 6 and 1?

Regards,

هل كانت مفيدة؟

المحلول

I believe it's because the way you've constructed your CASE statement is testing the result of the lead() function against null for equality. Instead of using a construction like this:

case x when null then 1 else 0 end

Try this:

case when x is null then 1 else 0 end
مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top