I am running a market survey and all the survey data is saved in the database. I need a query for counting the number of rows in which option "1" is selected for question "1," option "2" for question "1" and so on for all questions and options. I need to specify a few conditions here. I have to match distinct IDs of three tables and display the result for each particular ID.
You've given very little information about your tables, so the following solution explains how to get the counts without going into details about how your tables are joined.
SELECT x.question_number , y.option_number , COUNT(*) AS number_of_rows FROM your joins go here GROUP BY x.question_number , y.option_number
The count that is produced for this particular GROUP BY clause is the count for every single unique combination of question_number and option_number values.
The only wrinkle on the solution is if certain option numbers were not chosen. You cannot count what's not there, if you're using an INNER JOIN; a LEFT OUTER JOIN should be used instead. But I cannot give you a sample query because I don't know what your three tables look like.
Dig Deeper on Oracle development languages
Related Q&A from Rudy Limeback
SQL expert Rudy Limeback explains how to sort an SQL UNION query using a special ORDERY BY sequence. Continue Reading
SQL expert Rudy Limeback explains how to formulate a query using an SQL SELECT statement from a non-existing table. Continue Reading
An SQL expert explains how using a LEFT OUTER JOIN query can retrieve zero row counts in SQL. Continue Reading