I have 3 different claims in my dataset.
ClaimStatus
can be only 1 or 0. Which indicates Open (0) or Closed (1).
How to eliminate those claims that have the last ClaimStatus = 1
.
I tried to use Last_Value
function in my WHERE
clause but got an error:
Windowed functions can only appear in the SELECT or ORDER BY clauses
Query result should return only Claim2
, because ClaimStatus
is still open:
Code:
declare @TempTable table
(
ClaimNumber varchar(50),
ActivityID int,
Activity varchar(50),
ActivityDate datetime,
ClaimStatus int
)
insert into @TempTable
values ('Claim1', 0, 'ClaimCreated', '2018-05-04 10:58:56.510', 0),
('Claim1', 4, 'ReserveCreated', '2018-05-09 09:52:52.327', 0),
('Claim1', 6, 'PaymentCreated', '2018-05-15 13:17:47.920', 0),
('Claim1', 8, 'ClaimClosed', '2018-11-01 10:53:00.087', 1),
('Claim2', 0, 'ClaimCreated', '2018-05-04 10:58:56.510', 0),
('Claim2', 4, 'ReserveCreated', '2018-05-09 09:52:52.327', 0),
('Claim2', 6, 'PaymentCreated', '2018-05-15 13:17:47.920', 0),
('Claim3', 0, 'ClaimCreated', '2018-05-04 10:58:56.510', 0),
('Claim3', 4, 'ReserveCreated', '2018-05-09 09:52:52.327', 0),
('Claim3', 6, 'PaymentCreated', '2018-05-15 13:17:47.920', 0),
('Claim3', 8, 'ClaimClosed', '2018-11-01 10:53:00.087', 1)
select *
from @TempTable
where LAST_VALUE(ClaimStatus) over (partition by ClaimNumber order by ActivityDate desc) <> 1