Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Count column reponses then order 1

Status
Not open for further replies.

dkemas

Programmer
Mar 22, 2012
70
GB
I have a table with a set of questions as columns, the answers to the questions are either 1, 2 or 3 i.e.

id | q1 | q2 | q3 | q4 | q5
1 | 1 | 1 | 1 | 2 | 2
2 | 1 | 2 | 2 | 2 | 2
3 | 1 | 1 | 2 | 1 | 2
4 | 1 | 1 | 2 | 1 | 2

I need to work out how many 2's there are for each question, then identify the top 3 questions with the most 2's and say how many there are.

From my example above I would output

Top 3 questions with the most 2's

q5 = 4
q3 = 3
q4 = 2

Can I do this with sql alone?

Thanks
 
Can I do this with sql alone?

Yes.

but your sql table structure is going to make this very difficult to do.

also bear in mind that you are trying to return an aggregate of all the tables and a number of rows from the tables. this is apples and oranges.

the query below will give you what you want but, by necessity, the total across all rows is included repetitively in each row in column 1. beyond either (i) two queries or (ii) concatenating the cols 2 and 3 across all rows, i see no alternative.

Code:
SELECT 
	(SELECT SUM(c.num)
	FROM
	(
		SELECT 'q1' as question, count(*) as num from questions where q1=2
		UNION
		SELECT 'q2', count(*) from questions where q2=2
		UNION
		SELECT 'q3', count(*) from questions where q3=2
		UNION
		SELECT 'q4', count(*) from questions where q4=2
		UNION
		SELECT 'q5', count(*) from questions where q5=2
	) as c
	) as total2s,
	a.question,
	a.num
FROM
(SELECT 'q1' as question, count(*) as num from questions where q1=2
UNION
SELECT 'q2', count(*) from questions where q2=2
UNION
SELECT 'q3', count(*) from questions where q3=2
UNION
SELECT 'q4', count(*) from questions where q4=2
UNION
SELECT 'q5', count(*) from questions where q5=2
) as a
order by a.num desc limit 3
 
Thanks for the help that works great, I'd also need to do the same for a time range.

Basically the 'questions' table has a 'itemid' column, this links to the 'item' table id column. The item table contains a date field, here's the structure

questions table

id | q1 | q2 | q3 | q4 | q5 | itemid

items table

id | datesubmitted

So ion essence I need to add to the query

JOIN items ON questions.itemid = items.id

and add a where clause somewhere to say

WHERE items.datesubmitted between '01-01-2013' and '01-12-2013'

Where / how would I add this?

Thanks
 
that is even more illogical. there is a one to one relationship between the date and the id row in the items. that is a time when then join could be avoided and the column be part of your table.

so you would have

Code:
table 1
submissionID, dateSubmitted, candidateID

table 2
submissionID, questionID, questionResponse

table 3
candidateID, candidateName, candidateEmail

if you had a structure like this, your flexibility in performing statistical analysis, or any kind of data mining and/or retrieval would be massively simplified.

as you can see by this query ....

Code:
SELECT 
	(SELECT SUM(c.num)
	FROM
	(
	SELECT 'q1' as question, count(*) as num from questions q
	join items i on i.id = q.id 
	where q.q1=2 and year(i.datesubmitted) = 2013
	UNION
	SELECT 'q2', count(*) from questions q 
	join items i on i.id = q.id 
	where q.q2=2 and year(i.datesubmitted) = 2013
	UNION
	SELECT 'q3', count(*) from questions q 
	join items i on i.id = q.id 
	where q.q3=2 and year(i.datesubmitted) = 2013
	UNION
	SELECT 'q4', count(*) from questions q 
	join items i on i.id = q.id 
	where q.q4=2 and year(i.datesubmitted) = 2013
	UNION
	SELECT 'q5', count(*) from questions q 
	join items i on i.id = q.id 
	where q.q5=2 and year(i.datesubmitted) = 2013
	) as c) as total2s,
	a.question,
	a.num
FROM

(	
	SELECT 'q1' as question, count(*) as num from questions q
	join items i on i.id = q.id 
	where q.q1=2 and year(i.datesubmitted) = 2013
	UNION
	SELECT 'q2', count(*) from questions q 
	join items i on i.id = q.id 
	where q.q2=2 and year(i.datesubmitted) = 2013
	UNION
	SELECT 'q3', count(*) from questions q 
	join items i on i.id = q.id 
	where q.q3=2 and year(i.datesubmitted) = 2013
	UNION
	SELECT 'q4', count(*) from questions q 
	join items i on i.id = q.id 
	where q.q4=2 and year(i.datesubmitted) = 2013
	UNION
	SELECT 'q5', count(*) from questions q 
	join items i on i.id = q.id 
	where q.q5=2 and year(i.datesubmitted) = 2013
) as a
order by a.num desc limit 3
 
in fact that's so over the top that i was tempted to write a stored procedure that would move the data to a properly constructed set of temp tables, query them normally and then return the result. quite possible that it would be very significantly faster but without extensive dummy data i cannot try.

you could always create data mining tables and use stored triggers on your other tables to duplicate the data into a properly normalised structure for later analysis. this wouldn't negatively affect your pre-written scripts as the non-normalized tables would remain untouched and still receive and output data.
 
Thanks for the help, that worked perfectly.

I am hoping to get some time to redo the tables so expect a thread here on how to properly sort the tables (I already have lots of bed time reading on best practices etc)
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top