?
Solved

Use CF to "merge" two SQL Server queries

Posted on 2005-03-02
3
Medium Priority
?
1,163 Views
Last Modified: 2013-12-24
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 do this in ColdFusion? 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="GetAllMultipleChoiceResponses" datasource="quiz1">
      SELECT RespondentID, QuestionID, OptionValue AS Response
      FROM MultipleChoiceResponses, Respondents, MultipleChoiceOptions
      WHERE MultipleChoiceResponses.RespondentID = Respondents.ID
      AND MultipleChoiceResponses.OptionID = 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!
0
Comment
Question by:Sapphireblue
  • 2
3 Comments
 
LVL 8

Accepted Solution

by:
kyle1830 earned 2000 total points
ID: 13443561
use a union
<cfquery name="GetAllMultipleChoiceResponses" datasource="quiz1">
     SELECT RespondentID, QuestionID, OptionValue AS Response
     FROM MultipleChoiceResponses, Respondents, MultipleChoiceOptions
     WHERE MultipleChoiceResponses.RespondentID = Respondents.ID
     AND MultipleChoiceResponses.OptionID = MultipleChoiceOptions.ID
     AND Respondents.SurveyID = #GetSurveyID#
UNION
     SELECT RespondentID, QuestionID, AnswerText AS Response
     FROM TextResponses, Respondents
     WHERE TextResponses.RespondentID = Respondents.ID
     AND Respondents.SurveyID = #GetSurveyID#
</cfquery>

Kyle
0
 
LVL 1

Author Comment

by:Sapphireblue
ID: 13443647
yikes. that's what I was thinking of, not Intersect, which I realize now is a totally different thing.

le sigh. Thank you!
0
 
LVL 8

Expert Comment

by:kyle1830
ID: 13443669
youre welcome,  sometimes another pair of eyes help.

kyle
0

Featured Post

[Webinar] Database Backup and Recovery

Does your company store data on premises, off site, in the cloud, or a combination of these? If you answered “yes”, you need a data backup recovery plan that fits each and every platform. Watch now as as Percona teaches us how to build agile data backup recovery plan.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

What You Need to Know when Searching for a Webhost Provider
Media Temple is proud to announce our partnership with the Society of Digital Agencies (SoDA) as their exclusive hosting partner.
With just a little bit of  SQL and VBA, many doors open to cool things like synchronize a list box to display data relevant to other information on a form.  If you have never written code or looked at an SQL statement before, no problem! ...  give i…
How can you see what you are working on when you want to see it while you to save a copy? Add a "Save As" icon to the Quick Access Toolbar, or QAT. That way, when you save a copy of a query, form, report, or other object you are modifying, you…
Suggested Courses
Course of the Month8 days, 7 hours left to enroll

615 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question