If there is the possibility that the same test can be recorded against the same primary ID more than once, then you need to be a bit careful to ensure you pick up both tests having been done, rather than just one having been done twice. So, for example:
SELECT t.[Primary ID], t.[Secondary ID], t.Test, Flag =
CASE
WHEN NOT EXISTS (SELECT * FROM tests a WHERE a.[Primary ID] = t.[Primary ID] AND a.Test = 'A') THEN 0
WHEN NOT EXISTS (SELECT * FROM tests b WHERE b.[Primary ID] = t.[Primary ID] AND b.Test = 'B') THEN 0
ELSE 1
END
FROM tests t;
Or, using joins rather than sub-queries:
SELECT t.[Primary ID], t.[Secondary ID], t.Test,
Flag = CASE WHEN a.Test IS NOT NULL AND b.Test IS NOT NULL THEN 1 ELSE 0 END
FROM tests t
LEFT JOIN (
SELECT DISTINCT [Primary ID]
FROM tests
WHERE Test = 'A'
) a
ON a.[Primary ID] = t.[Primary ID]
LEFT JOIN (
SELECT DISTINCT [Primary ID]
FROM tests
WHERE Test = 'B'
) b
ON b.[Primary ID] = t.[Primary ID]