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
 General SQL Server Forums
 New to SQL Server Programming
 How to join

Author  Topic 

learner321
Starting Member

2 Posts

Posted - 2012-08-10 : 06:36:58
Hi friends I have one table having quiz, another having questions of quiz, radio button third having options for every quiz question and fourth having checkbox items as option for question in quiz.

A question can have either radio button (single option) choice or checkbox option choice. I have joined it but it giving result for either radioboxes or checkboxes. If I join for radioboxed then questions which are having option for that are only displaying, if I select question having checkbox as option is shwoing only questins having checkbox as option.

I want to select all questions if it has either radio button options or checkbox option. Please help.

SwePeso
Patron Saint of Lost Yaks

30421 Posts

Posted - 2012-08-10 : 06:48:37
Please send your current DML (table sctructure), and some proper sample data for us to play with.
Remember, you cannot access your local server!


N 56°04'39.26"
E 12°55'05.63"
Go to Top of Page

sunitabeck
Master Smack Fu Yak Hacker

5155 Posts

Posted - 2012-08-10 : 06:48:37
If you can post the queries that you have currently that would make it easier for someone to respond more appropriately. My guess would be that you need left joins, something like this:
SELECT
q.QuestionId,
CASE
WHEN q.QuestionType = 'SingleOption' THEN r.Answer
ELSE m.Answer
END AS Answer
FROM
Questions q
LEFT JOIN RadioButtonChoices r ON q.QuestionId = r.QuestionId
LEFT JOIN MultipleChoices m ON q.QuestionId = m.QuestionId;
Go to Top of Page

learner321
Starting Member

2 Posts

Posted - 2012-08-10 : 07:29:30
Both of you thanks for the answer. The structure and sample data is like this:
Table 1 Questions:
uid,question

Table 2 Single option:
uid, option1, quistionid
uid, option2, questionid

Table 3 Multiple Option:
uid, option1, quistionid
uid, option2, questionid

Example:
Question 1: Which SQL server do you like most (having only radio buttons):
o MSSQL
o MySQL
o Postgres


Question 2: Which languge do you use to program (having checkboxes):
- C#
- java
- php

I want to retrieve record like this, first line is header
Question Radio Button Column Checkbox Column
question 1 MSSQL NULL
question 1 MySQL NULL
question 1 Postgres NULL
question 2 NULL C#
question 2 NULL Java
question 2 NULL Php
Go to Top of Page

SwePeso
Patron Saint of Lost Yaks

30421 Posts

Posted - 2012-08-10 : 07:52:22
Why are the single option and multioption answers separated into two tables?


N 56°04'39.26"
E 12°55'05.63"
Go to Top of Page

SwePeso
Patron Saint of Lost Yaks

30421 Posts

Posted - 2012-08-10 : 07:56:26
[code]SELECT t1.Question,
t2.[Option] AS RadioAnswer,
t3.[Option] AS CheckAnswer
FROM dbo.Table1 AS t1
LEFT JOIN dbo.Table2 AS t2 ON t2.QuestionID = t1.QuestionID
LEFT JOIN dbo.Table3 AS t3 ON t3.QuestionID = t1.QuestionID
ORDER BY t1.uID,
COALESCE(t2.uID, t3.uID);[/code]


N 56°04'39.26"
E 12°55'05.63"
Go to Top of Page
   

- Advertisement -