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 2005 Forums
 Transact-SQL (2005)
 Questionairre + Version query

Author  Topic 

nduggan23
Starting Member

42 Posts

Posted - 2008-12-09 : 07:04:42
hi all, have the following tables. Im trying to get a query to return a list of questions that matches the question versions for the answers, plus any additional questions that they have yet to complete. i have a query to get the latest version of question, used for the first time a form is completed. its if a question version changes that i want to be able to show the old version in place of the new one when the form is loaded again.


CREATE TABLE [AFD].[Submission]
(
submissionID int NOT NULL IDENTITY(1, 1) PRIMARY KEY CLUSTERED,
submissionTitle varchar(200) NOT NULL
)ON [PRIMARY];



CREATE TABLE [AFD].[SubmissionQuestionnaire]
(
questionID int NOT NULL IDENTITY(1, 1) PRIMARY KEY CLUSTERED,
parentForm int NOT NULL,
versionID int NOT NULL,
latestVersion bit NOT NULL,
questItemName varchar (50) NOT NULL,
loadOrder smallint NULL
) ON [PRIMARY];



CREATE TABLE [AFD].[SubmissionAnswers]
(
ansID int NOT NULL IDENTITY(1, 1) PRIMARY KEY CLUSTERED,
submissionID int NOT NULL,
questionID int NOT NULL,
questItemName varchar (50) NULL
) ON [PRIMARY];



Gets me the latest questionairre.

SELECT distinct q.questItemName, q.loadOrder, q.questionID
FROM AFD.SubmissionQuestionnaire q
INNER JOIN AFD.refForm f ON f.refID = q.parentForm
WHERE
q.visible = 1 AND q.latestVersion = 1
ORDER BY q.loadOrder


So if i have the following questions in the SubmissionQuestionairre table:
1, 1, 1, TRUE, lblQ1, 1
2, 1, 1, TRUE, lblQ2, 2
3, 1, 1, FALSE, lblQ3, 3
4, 1, 2, TRUE, lblQ3, 3

And if i have the following answers for a SubmissionID 1 in the SubmissionAnswers table:
1, 1, 1, lblQ1
2, 1, 2, lblQ2
3, 1, 3, lblQ3

so when i try to load the questionairre for an existing submission i want the following result:
QuestionID, QuestItemName, LoadOrder
1, lblQ1, 1
2, lblQ2, 2
3, lblQ3, 3

for a new submission it should be:
1, lblQ1, 1
2, lblQ2, 2
4, lblQ3, 3

Anyone able to help?


visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2008-12-09 : 10:25:47
[code]SELECT sa.questionID,sa.questItemName,sq.loadOrder
FROM [AFD].[SubmissionAnswers] sa
JOIN [AFD].[SubmissionQuestionnaire] sq
ON sq.questionID =sa.questionID
UNION ALL
SELECT sq.questionID,sq.questItemName,sq.loadOrder
FROM [AFD].[SubmissionQuestionnaire] sq
LEFT JOIN [AFD].[SubmissionAnswers] sa
ON sq.questionID =sa.questionID
AND sq.LatestyVersion='TRUE'
WHERE sa.questionID IS NULL[/code]

Go to Top of Page

nduggan23
Starting Member

42 Posts

Posted - 2008-12-09 : 11:13:19
Cheers for the reply. ive tried that before but the result is incorrect from the union.

QuestionID, QuestItemName, LoadOrder
1, lblQ1, 1
2, lblQ2, 2
3, lblQ3, 3
4, lblQ3, 3

im trying to get rid of the questionID 4 from the list. not sure its possible using a union.
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2008-12-09 : 11:22:04
quote:
Originally posted by nduggan23

Cheers for the reply. ive tried that before but the result is incorrect from the union.

QuestionID, QuestItemName, LoadOrder
1, lblQ1, 1
2, lblQ2, 2
3, lblQ3, 3
4, lblQ3, 3

im trying to get rid of the questionID 4 from the list. not sure its possible using a union.




you're getting above output for which submission? i dont think 4 will come for submission 1
try this out

SELECT sq.questionID,sq.questItemName,sq.loadOrder
FROM [AFD].[SubmissionQuestionnaire] sq
JOIN [AFD].[SubmissionAnswers] sa
ON sq.questionID =sa.questionID
AND sa.submissionID=1
Go to Top of Page

nduggan23
Starting Member

42 Posts

Posted - 2008-12-10 : 06:53:33
For submission 1, that was the output. Initially i was not storing all questions in the answers table unless they actually had an answer. so to get around the issue of generating a questionnaire for an old form ive just stored all questionID's in the answer table regardless of if they have an answer or not. cheers for the help.
Go to Top of Page
   

- Advertisement -