convert row data into columns in sql server - sql

I am working to create a report for our users which provides the data as to what questions and answers users answered for a certain conference. The questions for each conference are unique, so I don't know in advance the number of questions.
My data is currently showing up as follows
I want the data to be laid out this way
currently I'm using max case to accomplish this, but as mentioned before all conferences have varying degrees of sessions, so I have to change my query every time for each conference to ensure that the correct question_id and session_id is being used.
There's gotta be a better way...please help.
Thanks in advance.

Related

SQL - User Stats table?

I am working on a multi-choice questions web-app. A user will have to answer 50 to 100 multiple-choice questions. The number of possible answers for each question can differ from 3 to 4 but there is only 1 correct answer.
I would like to create a page stats where the user can come back to the answered questions and see what answer he did select. I am not sure what would be the proper way to do that.
So far I have:
users table
questions table
answers table
I would organize the answers table like this:
In that way:
you will not have columns with null values,
you can have questions with more than 3-4 answers too without adding any extra column
you can have questions with multiple right answers too
Then, you will need a table, which holds the user's answers:
Now, you can easily construct a query, which shows the required data.

Tableau count values after a GROUP BY in SQL

I'm using Tableau to show some schools data.
My data structure gives a table that has all de school classes in the country. The thing is I need to count, for example, how many schools has Primary and Preschool (both).
A simplified version of my table should look like this:
In that table, if I want to know the number needed in the example, the result should be 1, because in only one school exists both Primary and Preschool.
I want to have a multiple filter in Tableau that gives me that information.
I was thinking in the SQL query that should be made and it needs a GROUP BY statement. An example of the consult is here in a fiddle: Database example query
In the SQL query I group by id all the schools that meet either one of the conditions inside de IN(...) and then count how many of them meet both (c=2).
Is there a way to do something like this in Tableau? Either using groups or sets, using advanced filters or programming a RAW SQL calculated fiel?
Thanks!
Dubafek
PS: I add a link to my question in Tableu's forum because you can download my testing workbook there: Tableu's forum question
I've solved the issue using LODs (specifically INCLUDE and EXCLUDE statements).
I created two calculated fields having the aggregation I needed:
Then I made a calculated field that leaves only the School IDs that matches the number of types they have (according with the filtering) with the number of types selected in the multiple filter (both of the fields shown above):
Finally, I used COUNTD([Condition]) to display the amounts of schools matching with at least the School types selected.
Hope this helps someone with similar issue.
PS: If someone wants the Workbook with the solution I've uploaded it in an answer in the Tableau Forum

getting average value from records sql

I am writing an app that is basically a survey and I've written almost everything but this last part where I need to get stats from the survey.
The table looks like this:
user_id|question_id|answer
I need to get the average for each answer while still knowing which question ID that average value is associated with. Keep in mind that there can be multiple answers for the same question and multiple and multiple questions with the same answer. The only thing unique is the user_id. The database is Oracle but im sure I can adapt the query if needed. Just need some help with getting started.
I was able to answer my own question.
Select avg (answer), question_id
From table
Group By question_id

Database design for an online quiz

I am designing an online math quiz for a college project and having some trouble with designing my database.
The basic idea of the website is as follows:
A teacher, once registered may log in and add questions to their account. They can choose between making the questions multiple choice OR true or false. They can also choose between making their questions public or private. (If they choose to make the questions public, other teachers may view the questions.)
At any time the teacher may create a quiz for their students using the questions in their private bank and / or questions from the public bank. Each question may be used in multiple quizzes.
The idea is that the students will later log in and do the quiz; their answers are stored and the teacher can generate reports and check how the students did individually / highest and lowest scoring questions etc.
I am having some trouble deciding how to store the quizzes and questions which I hope somebody may be able to help me with.
So far I have the following:
‘Question’ table with attributes: QuestionID, SubjectArea, Concept, QuestionText, TeacherID, QuestionType, PublicYorN
‘MCQuestions’ table with attributes: QuestionID, AnsA, AnsB, AnsC, AnsD, AnsE, CorrectAns
‘TorFQuestions’ table with attributes: QuestionID, CorrectAns
‘Quiz’ table with attributes: QuizID, CreationDate, TeacherID
I think I then need another table as follows:
‘QuizQuestions’ and the only attributes will be QuizID, QuestionID which together make a concatenated primary key.
I feel like I should have a separate table to store answers to questions and I'm not sure if I need to separate true or false questions and multiple choice questions as I have done above.
(Obviously there are other tables containing user data etc. but this is the part I’m concerned with.)
Any advice / input is greatly appreciated!
A simple yet flexible design would be something like this:
Questions table (id, text, correct answer id, all the other question related data)
Answers table (id, question id text, all the other answer related data)
Quiz table (id, text, all the other quiz related data)
Quiz questions table (quiz id, question id, question display order, other related data such as question weight might also be added)
Quiz results table (quiz id, question id, answer id, user id, all the other related data such as date of answer and such).
I think my only input to this open ended question is:
Combine MCQuestions and TorF Questions to have a different format. Answers table has 4 cols. QID, AnswerID,answer,Correct Y/N.
Then a true or false question has 2 rows - eg. QID=888, AID=1,Ans=TRUE,true. Next row is QID=888, AID=2,Ans=FALSE,false.
Multiple choice has several - so which is the bright yellowy thing? QID=889, AID=3,Ans=Moon,false. QID=889, AID=4,Ans=Sun,true. QID=889, AID=5,Ans=Mars,false.
Then you grab a list of answers and can populate a set of radio buttons with the QID and AID values. Then you are matching integers in your code rather than long text strings which will need to be passed around for matching. Might make it a bit easier in case of special chars in the answers or whatever and you're matching the quiz results to a unique key.
Apart from that minor change it's difficult to help without knowing any other constraints...
Nick

what is the best way to record users searches and count how many times it has been queried?

I am not sure what SQL is exactly, databases I think, would SQL be the thing to use?
I want it so that when a user types into a search box it displays a "did-you-mean" box, that also shows how many other people used that term but I will do later ;)
currently I just want the text to be saved into database (?) and also how many times it's been done.
(new to stackoverflow so sorry if format/whatever is bad)
You just need a three table named something like search_queries which has columns for: id, search_text, and count. Everytime somebody searches just increment the count column where search_text is equal to what they searched for. This is a really broad question though and you need to start by learning the basics and report back here with more specific questions.