Why is LAST_VALUE() not working in SQL Server? -
here's data have (note 1 entity id / employee id, there multiple. 1 entity id can have multiple employee ids under it):
select entityid, employeeid, payperiodstart, isfulltime dbo.payroll employeeid = 316691 , payperiodstart <= '12/31/2014' , payperiodend >= '1/1/2014';
i want grab last "isfulltime" value each entityid & employeeid combo.
i tried doing this:
select entityid, employeeid, last_value(isfulltime) on (partition entityid, employeeid order entityid, employeeid, payperiodstart) lastisfulltimevalue dbo.payroll employeeid = 316691 , payperiodstart <= '12/31/2014' , payperiodend >= '1/1/2014';
but i'm getting this:
the query should return 1 row each entityid / employeeid.
what doing wrong?
i believe looking use row_number()
, last value based on payperiodstart
date:
select t.entityid ,t.employeeid ,t.lastisfulltimevalue ( select entityid ,employeeid ,row_number() on ( partition entityid ,employeeid order payperiodstart desc ) rn ,lastisfulltimevalue dbo.payroll employeeid = 316691 -- put in outer query instead , payperiodstart <= '12/31/2014' , payperiodend >= '1/1/2014' ) t t.rn = 1;
Comments
Post a Comment