I tried to work out around this. But I think I am getting no where.
I have 3 tables:
This tale contains all questions and question types:
Table: Ref
id | type | info
==========================
1 SS Education
---------------------------
2 RB Gender
---------------------------
3 ST State
This table contains “options” for the questions in the above table ‘Ref`
Table: ref_ans
id | q_id | answer_text
===========================
1 1 Masters
---------------------------
2 1 Bachelors
---------------------------
3 1 Undergrad
---------------------------
4 2 Male
---------------------------
5 2 Female
---------------------------
6 2 Dont want to disclose
This table contains states (type =”ST” in table Ref)
Table: us_states
id | answer_text
===========================
1 Alaska
---------------------------
2 Alabama
---------------------------
3 Arkansan
---------------------------
4 Arizona
---------------------------
5 Baltimore
---------------------------
etc
The result I want is:
ref.id, ref_ans.id, ref.answer_text / us_states.answer_text
*for a given ref.question_id *.
And the condition is: If the question_id, for which the answers requested is ‘ST’, it should pull the answers from us_states, otherwise, it should pull from ref-ans table.
I tried this. Obviously, this did not work:
SELECT ref.id,
CASE WHEN ref.type = 'ST' THEN
(SELECT ID, answer_text FROM us_states )
ELSE
(SELECT id, answer_text FROM ref_ans)
END
FROM ref
WHERE ref.ID = <id>
Any ideas?
Try: