2014-02-26 52 views
1

我有窗函數簡單的查詢:如果數值在「OVER(按某事物排序)」組中爲「1」,如果爲「0」則爲「0」

select 
    productId, 
    is_last = row_number() over(order by productId) 
from [productstatuses] order by ProductID 

如何修改「is_last」獲得1作爲值,如果當前記錄是去年在「過(順序,按ProductID)」組和0,如果不是?

UPDATE:感謝對@TechDo,我的問題最終的解決方案是:

select 
    productId, 
    is_last = iif(count(*) over(order by productId)=row_number() over(order by productId), 1, 0) 
from [productstatuses] order by ProductID 

回答

2

請嘗試:

select 
    productId, 
    is_last = case when COUNT(*) OVER()=row_number() over(order by productId) then 1 else 0 end 
from [productstatuses] order by ProductID 
+0

謝謝!計數也必須用「(order by productId)」來覆蓋,以獲得我需要的結果。它的工作原理! :) – rufanov

相關問題