Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 SQL Server 2000 Forums
 SQL Server Development (2000)
 How do I order the results of an inner join based on a secondary table

Author  Topic 

AskSQLTeam
Ask SQLTeam Question

0 Posts

Posted - 2002-07-23 : 09:00:02
Holyshirt writes "I have three tables. Table A contains a master list for the order of some questions and an ID, table B contains questions each with a reference to table A for the order of those groups and table C contains the options associated with each question, also ordered according to a choicenumber.

How do I sort the result of an inner join between B and C (which gives me all the questions and their options) according to the order defined in A for the questions in B.

Table A has a GroupOrder field, table B has a QuestionOrder field and table C has a ChoiceOrder field.

eg. the following query returns the questions and choices based on the question order, not the greater order of the groups those questions are in.


SELECT * FROM questions B INNER JOIN questionchoices C ON C.questions_id = B.id WHERE B.surveys_id = 1 AND C.surveys_id=1 ORDER BY B.questionorder, C.choicenumber


How do I select (and ORDER BY) the group order from table A (referred to in table B as A.ChoiceGroups_ID) while still obtaining all the joined results from B and C, and odering them according to their local order within question ?

What I need to do is order the result by the order defined in A for the questions in B according to the GroupOrder in A referenced by the ID of that row from B (B.ChoiceGroups_ID = A.ID) which doesn't actually give me the A.GroupOrder that is associated with A.ID ?"

Page47
Master Smack Fu Yak Hacker

2878 Posts

Posted - 2002-07-23 : 09:36:45
Without DDL, it's impossible to provide you with working SQL code. However, it seems you need to join to table A so you can order by it. You can do this without including any of the columns in table A in your select list, unless you have a group by clause.

<O>
Go to Top of Page
   

- Advertisement -