Skip to content

SQL Example of First_Value Last_Value

clancyjane edited this page Sep 23, 2014 · 1 revision
select distinct
            CHILD
         , removal_dt
         , first_value(removal_dt) 
                    over (partition by CHILD  , year(removal_dt) 
            order by removal_dt asc) [first_removal_in_year]
        , last_value(removal_dt) over 
            (partition by child 
            , year(removal_dt)
        order by removal_dt asc 
        RANGE BETWEEN CURRENT ROW AND UNBOUNDED FOLLOWING)
from base.rptPlacement plc
where year(removal_dt) >=2000 
order by CHILD,removal_dt


in this example unbounded preceding works also

UNBOUNDED PRECEDING The range starts at the first row of the partition.
UNBOUNDED FOLLOWING The range ends at the last row of the partition.
CURRENT ROW range begins at the current row or ends at the current row
n PRECEDING or n FOLLOWING The range starts or ends n rows before or after the current row

Clone this wiki locally