×
INTELLIGENT WORK FORUMS
FOR COMPUTER PROFESSIONALS

Are you a
Computer / IT professional?
Join Tek-Tips Forums!
• Talk With Other Members
• Be Notified Of Responses
• Keyword Search
Favorite Forums
• Automated Signatures
• Best Of All, It's Free!

*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

#### Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

# Count column reponses then order

## Count column reponses then order

(OP)
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

### RE: Count column reponses then order

#### Quote:

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 

### RE: Count column reponses then order

(OP)
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

### RE: Count column reponses then order

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 

### RE: Count column reponses then order

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.

### RE: Count column reponses then order

(OP)
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)

#### Red Flag This Post

Please let us know here why this post is inappropriate. Reasons such as off-topic, duplicates, flames, illegal, vulgar, or students posting their homework.

#### Red Flag Submitted

Thank you for helping keep Tek-Tips Forums free from inappropriate posts.
The Tek-Tips staff will check this out and take appropriate action.

Close Box

# Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

• Talk To Other Members
• Notification Of Responses To Questions
• Favorite Forums One Click Access
• Keyword Search Of All Posts, And More...

Register now while it's still free!