Is there a way to add the last line in the code?
declare @requestorid int = 44997,
@recordid int = 152810,
@minMax int = 0
SELECT a.dateentered,
c.namekey,
b.initials
FROM dbo.tbltrkrecordlog AS a
INNER JOIN (SELECT Max(t.recordid) AS recordid,
t.customfieldid,
case when @minMax =1 then max(t.dateentered) else min(t.dateentered) end changeDate,
max(SUBSTRING(e.firstname, 1, 1) + SUBSTRING(e.middleinitial, 1, 1) + SUBSTRING(e.lastname, 1, 1)) initials
FROM dbo.tbltrkrecordlog t
left join tblemployee e on e.id = t.employeeid
WHERE ( t.recordid = @recordid )
GROUP BY t.customfieldid) AS b
ON a.customfieldid = b.customfieldid
AND a.dateentered = b.changeDate
INNER JOIN dbo.tbltrkcustomfield AS c
ON a.customfieldid = c.id
where c.namekey = 'hprmprcorrectiveactionsneededtakennotes'
--and case when @minMax =1 then max(t.dateentered) else min(t.dateentered) end