Author |
Topic |
slihp
Yak Posting Veteran
61 Posts |
Posted - 2013-07-17 : 05:19:25
|
We have a risk assessment questionnaire program this feeds off a Question table and an Answer table and save into a results table.Question table-----------------------ID | Question-----------------------1 | Question 1 2 | Question 2 3 | Question 3 4 | Question 4 5 | Question 5 Answer table-----------------------ID |QuestionID |Answers-----------------------1 | 1 |Yes2 | 1 |NO3 | 2 |True4 | 2 |False5 | 3 |Pass6 | 3 |Refer7 | 3 |Fail8 | 4 |Mostly9 | 4 |Never10 | 5 |Blue11 | 5 |GreenResults table--------------------------ID | QuestionID | AnswerID-----------------------1 | 1 | 22 | 2 | 43 | 3 | 74 | 4 | 85 | 5 | 11 The user is presented with a series of questions and answers. Each question can have one answer from a possible two or three. I want to present the user with questionnaire results in the below format (i.e. question with answer below)Question 1NoQuestion 2FalseQuestion 3FailQuestion 4MostlyQuestion 5GreenProblem is the application I have to use to present the results will only read from a SQL view. I don’t use views to much but im aware they have limitations to what you can do. Is it possible to construct sql for a view that will present the results in the format I want?Many thanks |
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2013-07-17 : 05:40:45
|
[code]CREATE VIEW yourViewNameASSELECT Val FROM(SELECT r.ID,q.Question,a.AnswersFROM Results rINNER JOIN Question qON q.QuestionID = r.QuestionID INNER JOIN Answer aON a.AnswerID = r.AnswerIDAND a.QuestionID = r.QuestionID )tUNPIVOT(Val FOR Descr IN (Question,Answers))uORDER BY ID[/code]------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/https://www.facebook.com/VmBlogs |
|
|
khtan
In (Som, Ni, Yak)
17689 Posts |
Posted - 2013-07-17 : 05:52:44
|
you can't have the ORDER BY in a VIEW, unless TOP is usedCREATE VIEW yourViewNameASSELECT Val FROM(SELECT r.ID,q.Question,a.AnswersFROM Results rINNER JOIN Question qON q.QuestionID = r.QuestionID INNER JOIN Answer aON a.AnswerID = r.AnswerIDAND a.QuestionID = r.QuestionID )tUNPIVOT(Val FOR Descr IN (Question,Answers))uORDER BY ID KH[spoiler]Time is always against us[/spoiler] |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2013-07-17 : 06:13:26
|
Yep..thats true..that problem with fitting the CREATE VIEW part just before posting CREATE VIEW yourViewNameASSELECT TOP 100 PERCENT Val FROM(SELECT r.ID,q.Question,a.AnswersFROM Results rINNER JOIN Question qON q.QuestionID = r.QuestionID INNER JOIN Answer aON a.AnswerID = r.AnswerIDAND a.QuestionID = r.QuestionID )tUNPIVOT(Val FOR Descr IN (Question,Answers))uORDER BY ID ------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/https://www.facebook.com/VmBlogs |
|
|
slihp
Yak Posting Veteran
61 Posts |
Posted - 2013-07-17 : 07:00:20
|
GREAT, thats exactly what i was after, thanks lots.SELECT TOP 100 PERCENT Results FROM(SELECT qr.ID, qr.InstanceID, q.QUESTION, qa.ANSWERFROM dbo.HM_RAR_QUESTIONNAIRE_RESULTS qrINNER JOIN dbo.HM_RAR_QUESTIONS q ON q.ID=qr.QuestionIDINNER JOIN dbo.HM_RAR_ANSWERS qa ON qa.ID=qr.AnswerID AND qa.QUESTION_ID=q.IDWHEREqr.QuestionID<>1 )tUNPIVOT(Results FOR Descr IN (Question,Answer))uORDER BY IDproblem though i noticed when i implemented it, the results table holds all instances of all questionnaires. my applications form only want to show the question and answers relating to the questionnaire selected. the form can do this by storing an instance id that it gets passed. so i realy need to view to present the instanceid both the question and answer belong to in another column.again is this possible? |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2013-07-17 : 07:08:37
|
quote: Originally posted by slihp GREAT, thats exactly what i was after, thanks lots.SELECT TOP 100 PERCENT Results FROM(SELECT qr.ID, qr.InstanceID, q.QUESTION, qa.ANSWERFROM dbo.HM_RAR_QUESTIONNAIRE_RESULTS qrINNER JOIN dbo.HM_RAR_QUESTIONS q ON q.ID=qr.QuestionIDINNER JOIN dbo.HM_RAR_ANSWERS qa ON qa.ID=qr.AnswerID AND qa.QUESTION_ID=q.IDWHEREqr.QuestionID<>1 )tUNPIVOT(Results FOR Descr IN (Question,Answer))uORDER BY IDproblem though i noticed when i implemented it, the results table holds all instances of all questionnaires. my applications form only want to show the question and answers relating to the questionnaire selected. the form can do this by storing an instance id that it gets passed. so i realy need to view to present the instanceid both the question and answer belong to in another column.again is this possible?
Its possible. Is instanceid one generated by form or is it stored in table?------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/https://www.facebook.com/VmBlogs |
|
|
slihp
Yak Posting Veteran
61 Posts |
Posted - 2013-07-17 : 07:13:12
|
InstnaceID is stored in the table, you set the form with a parameter, in this case instanceID, when the form is displayed it is passed the instanceID and uses this to filter out all the other questionnaire instance from the view. |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2013-07-17 : 07:18:33
|
quote: Originally posted by slihp InstnaceID is stored in the table, you set the form with a parameter, in this case instanceID, when the form is displayed it is passed the instanceID and uses this to filter out all the other questionnaire instance from the view.
Oh ok...You cant pass parameters to a viewFor that you need to make it into a procedure likeCREATE PROC yourProcName@InstanceID intASSELECT TOP 100 PERCENT Results FROM(SELECT qr.ID, qr.InstanceID, q.QUESTION, qa.ANSWERFROM dbo.HM_RAR_QUESTIONNAIRE_RESULTS qrINNER JOIN dbo.HM_RAR_QUESTIONS q ON q.ID=qr.QuestionIDINNER JOIN dbo.HM_RAR_ANSWERS qa ON qa.ID=qr.AnswerID AND qa.QUESTION_ID=q.IDWHEREqr.QuestionID<>1 )tUNPIVOT(Results FOR Descr IN (Question,Answer))uORDER BY IDGOthen call it likeEXEC YourProcName <InstanceIDvalue>the <InstanceIDvalue> will be passed from your form ------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/https://www.facebook.com/VmBlogs |
|
|
slihp
Yak Posting Veteran
61 Posts |
Posted - 2013-07-17 : 07:28:21
|
No no i knew that. I dont need to pass anything to the view. as long as the view has an InstancedID colum exposing the instanceid for each question and answer the form some how manages to filter out the other results (i can link the parameter passed to the form to the instanceid exposed by the view and the form dose the rest internally)the form is an asp page created by our third party vendor, it can only run off view, i cant use sp's its rubbish but i have to use it :-(. so if i can get the view to display another colum showing the instanceid each question and answer im sure this would work as i intended!! |
|
|
slihp
Yak Posting Veteran
61 Posts |
Posted - 2013-07-17 : 07:36:08
|
so would this work, it seems to give me the output im afterSELECT InstanceID, Results FROM(SELECT qr.ID, qr.InstanceID, q.QUESTION, qa.ANSWERFROM dbo.HM_RAR_QUESTIONNAIRE_RESULTS qrINNER JOIN dbo.HM_RAR_QUESTIONS q ON q.ID=qr.QuestionIDINNER JOIN dbo.HM_RAR_ANSWERS qa ON qa.ID=qr.AnswerID AND qa.QUESTION_ID=q.IDWHEREqr.QuestionID<>1 )tUNPIVOT(Results FOR Descr IN (Question,Answer))AS u |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2013-07-17 : 11:21:29
|
Oh ok...cool------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/https://www.facebook.com/VmBlogs |
|
|
slihp
Yak Posting Veteran
61 Posts |
Posted - 2013-07-18 : 05:19:49
|
That wroked guys, thanks a lot... |
|
|
|