| 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, 12, 1, 1, TRUE, lblQ2, 23, 1, 1, FALSE, lblQ3, 34, 1, 2, TRUE, lblQ3, 3And if i have the following answers for a SubmissionID 1 in the SubmissionAnswers table:1, 1, 1, lblQ12, 1, 2, lblQ23, 1, 3, lblQ3so when i try to load the questionairre for an existing submission i want the following result:QuestionID, QuestItemName, LoadOrder1, lblQ1, 12, lblQ2, 23, lblQ3, 3for a new submission it should be:1, lblQ1, 12, lblQ2, 24, lblQ3, 3Anyone 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.loadOrderFROM [AFD].[SubmissionAnswers] saJOIN [AFD].[SubmissionQuestionnaire] sqON sq.questionID =sa.questionID UNION ALLSELECT sq.questionID,sq.questItemName,sq.loadOrderFROM [AFD].[SubmissionQuestionnaire] sqLEFT JOIN [AFD].[SubmissionAnswers] saON sq.questionID =sa.questionID AND sq.LatestyVersion='TRUE'WHERE sa.questionID IS NULL[/code] |
 |
|
|
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, LoadOrder1, lblQ1, 12, lblQ2, 23, lblQ3, 34, lblQ3, 3im trying to get rid of the questionID 4 from the list. not sure its possible using a union. |
 |
|
|
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, LoadOrder1, lblQ1, 12, lblQ2, 23, lblQ3, 34, lblQ3, 3im 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 1try this outSELECT sq.questionID,sq.questItemName,sq.loadOrderFROM [AFD].[SubmissionQuestionnaire] sqJOIN [AFD].[SubmissionAnswers] saON sq.questionID =sa.questionID AND sa.submissionID=1 |
 |
|
|
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. |
 |
|
|
|
|
|