There is a simpler solution.
Create tables with sample data:
drop table if exists #person
go
create table #person (
personid int ,
name varchar(100)
)
go
insert into #person (personid, name) VALUES
(1,'Manu'), (2,'Obed'), (3,'Jimmy'), (4,'Kimel'), (5,'Marry')
go
select * from #person ORDER BY personid
go
drop table if exists #FullHistory
go
create table #FullHistory(
PersonId int ,
Activity Varchar(10),
Date_Activity Date
)
go
insert into #FullHistory(PersonId, Activity, Date_Activity) VALUES
(1,'Reg','2019-01-01'), (1,'DeReg','2019-01-02'), (1,'Reg','2019-01-03'), (1,'Grad','2019-01-04'),
(2,'Reg','2019-01-01'), (2,'Pass','2019-01-02'), (2,'Grad','2019-01-03'),
(3,'Reg','2019-01-01'), (3,'Pass','2019-01-02'),
(4,'Reg','2019-01-01'),
(5,'Reg','2019-01-01'), (5,'DeReg','2019-01-02')
go
select * from #FullHistory
go
Get the last activity per person from the history table. If that last activity = 'Reg' you have found what you were looking for.
;WITH CTE_fullHistory AS(
SELECT *
, ROW_NUMBER() OVER (PARTITION BY PersonId ORDER BY Date_Activity DESC) as RowNum
FROM #FullHistory
)
SELECT *
FROM CTE_fullHistory FH
INNER JOIN #person as P
ON FH.PersonId = P.personid
WHERE FH.RowNum = 1
AND FH.Activity = 'Reg'