I am try to create a SQL query for the user should have revoke entry in Database for the respective grant access for that study. For ex, if the user is having total count of 20 GRANT records then the user should have total count of REVOKE records. If not we need to enter the missing records.
I have created the following query. The query is working fine for the user who is having single Grant record for study but if the user is having Multiple grant record for particular study its not working.
For ex. User A is having two GRANT records and 1 REVOKE records for study ABC. the below query is not showing any output since the REVOKE record is already listed for that study.
But we have only one REVOKE records for that study. So I want to display that MISSING GRANT record
SELECT Getdate() [RequestedDate],
Getdate() [ApprovedDate],
A.requestedfor,
'XXX' [ApprovedBy],
A.projectnumber,
'Revoke' [AccessRequestType]
FROM [dbo].[Table1] A
WHERE A.requestedfor = 1234
AND A.accessrequesttype = 'Grant'
AND NOT EXISTS (SELECT *
FROM [dbo].[Table1] B
WHERE B.requestedfor =1234
AND B.accessrequesttype = 'Revoke'
AND A.projectnumber = B.projectnumber
AND A.accessgroup = B. accessgroup
AND A.RequestedFor = B. RequestedFor
)