0

I have two tables like this:

ID | Parent_Question_ID | AnswerID

Then another table:

ID | AnswerID | Sub_Question_ID

In here Parent_Question_ID and Sub_Question_ID are all Question_IDs. and AnswerIDs are also the same.

The relationship is that a "ParentQuestion" will have 1 or more answers and answers will have one more "SubQuestion"s, which can again have 1 or more answers and so on. If a "SubQuestion" has answers it will be stored as a Parent_Question_ID with Answer_ID in table 1.

I want to write a method to get all the children of a ParentQuestionID. I tried several solutions but still no luck. Any help would be greatly appreciated.

pushkin
  • 9,575
  • 15
  • 51
  • 95
Nats De
  • 121
  • 1
  • 9
  • You need to use a recursive algorithm because a Sub_Question can also have a Sub_Question. I have do this lot of times with two column tables : 1) Parent ID 2) Child ID. – jdweng May 28 '18 at 11:56
  • Writing the recursion has been the problem.. Since these are in 2 tables and Parent and Child Id that are different from each other – Nats De May 28 '18 at 12:03
  • Are the two tables identical? They should be except sorted differently. Then you only need one table to make you recursive method. – jdweng May 28 '18 at 12:11
  • @NatsDe Can you check if this could help https://stackoverflow.com/questions/14518090/recursive-query-in-sql-server – Neha Shettar May 28 '18 at 12:23
  • This isn't very clear. `AnswerID`s are same as what? Is the first table Questions and the second table Answers? Show some sample data. Do the answers form a chain? Do the questions form a chain or a tree? How do you know when the chain ends? – NetMage May 29 '18 at 23:36

0 Answers0