'How to insert user access missing records for the user

enter image description here

I am trying to insert user access missing record. I have used Exist statement to get the missing record. But we are facing problem when user is Granted access then revoked and then granted for same project.

GRANT ->REVOK->GRANT

In this case we cant able to get the record. Since we already have revoke access listed for that user in table.

Kindly advise

query :

   SELECT Getdate() [RequestedDate],
   Getdate() [ApprovedDate],
   A.requestedfor,
   '1234'    [ApprovedBy],
   A.ProjectNo,
  
   'Revoke'  [AccessRequestType]

FROM   [dbo].[projectaccessrequests] A
WHERE  A.requestedfor = 123
   AND A.accessrequesttype = 'Grant'
   AND NOT EXISTS (SELECT *
                   FROM   [dbo].[projectaccessrequests] B
                   WHERE  B.requestedfor = 123
                          AND B.accessrequesttype = 'Revoke'
                          AND Isnull (A.projectno, 0) =
                              Isnull (B.projectno, 0)
                          ) 


Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source