How could I make this query better?
SELECT ClientID, BatchID, jobid, subjobid, count(clientid) as Total
FROM data with(nolock)
WHERE batchid in (select BatchID from data with(nolock) where lookupcode = '111111111111')
and clientid in (select ClientID from data with(nolock) where lookupcode = '111111111111')
and jobid in (select jobid from data with(nolock) where lookupcode = '111111111111')
and subjobid in (select subjobid from data with(nolock) where lookupcode = '111111111111')
and entrytype <> 'C'
and entrytype <> 'M'
group by clientid,BatchID, jobid, subjobid
Try a CTE:
Your original solution however does not implement the condition you state in the comments:
since you do not check for the combination of job and subjob.
If you need to combine some fields for exact matches, try a condition like this
Since we don’t have a table structure and sample data, it’s hard for us to check whether our solutions are correct.