Sapphireblue
asked on
"Merge" two SQL Server 2000 queries, like Oracle INTERSECT
I have two SQL Server queries I'm manipulating in ColdFusion that I really need to be able to manipulate as one query.
If I were running Oracle, INTERSECT in the query would do what I want in a snap, but no such luck.
Any graceful way to fake an INTERSECT in SQL Server? I expect large datasets, so it would need to be pretty efficient. The two queries are as follows. You can see their results columns are exactly the same.
<cfquery name="GetAllMultipleChoice Responses" datasource="quiz1">
SELECT RespondentID, QuestionID, OptionValue AS Response
FROM MultipleChoiceResponses, Respondents, MultipleChoiceOptions
WHERE MultipleChoiceResponses.Re spondentID = Respondents.ID
AND MultipleChoiceResponses.Op tionID = MultipleChoiceOptions.ID
AND Respondents.SurveyID = #GetSurveyID#
</cfquery>
<cfquery name="GetAllTextResponses" datasource="quiz1">
SELECT RespondentID, QuestionID, AnswerText AS Response
FROM TextResponses, Respondents
WHERE TextResponses.RespondentID = Respondents.ID
AND Respondents.SurveyID = #GetSurveyID#
</cfquery>
Bonus points (I can do that, right?) for being able to sort the resulting/combined query by RespondentID ASC, QuestionID ASC!
If I were running Oracle, INTERSECT in the query would do what I want in a snap, but no such luck.
Any graceful way to fake an INTERSECT in SQL Server? I expect large datasets, so it would need to be pretty efficient. The two queries are as follows. You can see their results columns are exactly the same.
<cfquery name="GetAllMultipleChoice
SELECT RespondentID, QuestionID, OptionValue AS Response
FROM MultipleChoiceResponses, Respondents, MultipleChoiceOptions
WHERE MultipleChoiceResponses.Re
AND MultipleChoiceResponses.Op
AND Respondents.SurveyID = #GetSurveyID#
</cfquery>
<cfquery name="GetAllTextResponses"
SELECT RespondentID, QuestionID, AnswerText AS Response
FROM TextResponses, Respondents
WHERE TextResponses.RespondentID
AND Respondents.SurveyID = #GetSurveyID#
</cfquery>
Bonus points (I can do that, right?) for being able to sort the resulting/combined query by RespondentID ASC, QuestionID ASC!
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
How to mimic INTERSECT and EXCEPT in T-SQL
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dnsqlmag03/html/Set-OperationAlternatives.asp
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dnsqlmag03/html/Set-OperationAlternatives.asp
Good link Anthony...thanks
ASKER
Sorry acperkins. I am clearly a dork.
ASKER
So yeah, I'm feeling stupid, but thanks a lot for your help :D