Click to See Complete Forum and Search --> : SQL SubQueries


Judgey
August 21st, 2001, 03:49 AM
Good Morning Guru's,

Could somebody please switch the lights on for me on this one. I have been trying to get my head round subqueries without much success and I really want to learn how to do them, so so could anybody resolve this one for me so I can see how you do it.

Basically I have my first Query which groups together a list using the SQL statement below ;

SELECT TestAnswers.TestNumber, TestAnswers.QuestionNo, TestAnswers.AnsweredType FROM TestAnswers GROUP BY TestAnswers.TestNumber, TestAnswers.QuestionNo, TestAnswers.AnsweredType HAVING (((TestAnswers.TestNumber)=1644));

This gives the results as follows ;

TestNumber, QuestionNo, AnsweredType
1644 , 126 , 0
1644 , 131 , 0
1644 , 132 , 1
1644 , 145 , 2

I then need to perform a simple Count query on this just to return the number of each AnsweredType;

eg.

AnsweredType CountAnsweredType
0 2
1 1
2 1

I can't just do a count query in the first instance as there could be 6 records for each question so it inflates the count hence the need to do a GROUP BY Query first.

Please help . . . .




Quote of the Day;

Work is necessary for man. Man invented the alarm clock.

phunkydude
August 21st, 2001, 04:28 PM
The GROUP BY and HAVING clauses need only be used when you have an aggregate function in the select clause. You're quite close so I'll leave you up to getting the answer wrapped too. Hint: I don't see any need for a subquery here either.

michi
August 21st, 2001, 04:49 PM
=====
SELECT TestNumber, QuestionNo, AnsweredType, count(*) CountAnsweredType
FROM TestAnswers
WHERE TestNumber=1644
GROUP BY TestNumber, QuestionNo,AnsweredType
====

Regards,

Michi