getting average value from records sql - 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

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

SQL query for the number of cases when a value of column1 (non-unique) can't be found within any record where column2 meets a basic criteria

I am doing a beginners' SQL tutorial and I started to wonder whether a simple SQL query on this table: http://www.sqlcourse2.com/items_ordered.html could tell the number of items (also 1) which have only been purchased more items at a time, so there is no record which contains the quantity column with a value of 1 AND the item. I am really beginner at this so please try to keep it simple.
Thank you in advance!
Welcome to the fascinating world of SQL.
Well - I'm not giving you the answer, but a hint (after all, it's a training and your own thinking and finding the solution would be the best way for you to learn something new).
The way you formulate your question is somewhat puzzling.
When I combine what you ask with what is possible with SQL, the question that would make sense to me would be that you need to list (or count, I did not understand that very well) the items (or the complete rows in the table with matching item, that was not clear either), that were never sold with a quantity of 1.
If that's what you need, you will need a subselect to get all distinct items that were sold with a quantity of 1, and select the rows from your base table whose item value is not in the list you get from the subselect.
Do you need more hints?
Marco

SQL Server How to Build Query?

I'm kind new in this forum but I'm stuck in a problem and I need our help.
I Have one table with several lines where each line represent one project, then in another table I'll have many tasks that need to be done in each project, each task would have a percentage of at what level is, and the result of this two tables is one table where I'll have the process Id and also the percentage of accomplished with the average of the last entries of every tasks values.
I can't figured out the query that needs to be done to have the result that I want.
Can anyone help me? You can see follow the link bellow to see tables and the result that I want.
Table iamges
I didnt understand the colors of rows you used, but with your description, i think this is the query you are looking for:
select P.id_Proceso, P.SubProceso, avg(R.estado)
from Processos P
join Registros R
on P.ID = R.Id_processo
group by P.id_Proceso, P.SubProceso

convert row data into columns in sql server

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.