Hi,
I am trying to get sets of 2 rows from a table in my database. I only want the rows if they both in the table. I have tried to use 2 Exists clauses but it seems that when I do that, I get no results. If I replace the second "AND EXISTS" with "OR EXISTS", I get results but they are not in the pairs that I want.
Basically, from the query below, I want to return the rows that exist in the table where there is a "SurveySetSnapshotID" for each of the "StatFlagID"
SELECT DISTINCT S.SurveySetSnapshotID, S.SurveySetDefinitionID, S.StatFlagID, S.RespondentCode, S.QuestionsCompleted
FROM OpSurveySetSnapshot AS S
WHERE S.RespondentCode IS NOT NULL AND S.QuestionsCompleted =1 AND S.Deleted =0
AND EXISTS
(
SELECT * FROM OpSurveySetSnapshot S1
WHERE S1.StatFlagID = '{233711e2-b972-4fe4-998e-fb197a5ccd8b}' AND S1.Deleted = 0 AND S1.QuestionsCompleted =1
AND S.SurveySetSnapshotID = S1.SurveySetSnapshotID
 
AND EXISTS
(
SELECT * FROM OpSurveySetSnapshot S2
WHERE S2.StatFlagID = '{c4ab69c8-0865-4940-bc63-3d2aae0f2581}' AND S2.Deleted = 0 AND S2.QuestionsCompleted =1
AND S.SurveySetSnapshotID = S2.SurveySetSnapshotID
 
So the results should be like:
Snapshot 1 - Definition 1 - {233711e2-b972-4fe4-998e-fb197a5ccd8b}
Snapshot 2 - Definition 1 - {c4ab69c8-0865-4940-bc63-3d2aae0f2581}
Thanks for the Help,
Matt