|
-
Apr 8th, 2002, 06:58 PM
#1
Thread Starter
PowerPoster
SQL question...
I have a (1 to many) table which can contain an arbitrary number of fields, all linked to one field in another table.
i.e - table [Answers] may have 5 answers for a single question in table [Questions] - you with me?
What would be appropropriate SQL to find the LARGEST number of answers to a question?
I'm thinking something like "SELECT MAX(COUNT(answerText)) WHERE questionNumber = 1"
?
-----------------------------------------
-RJ
[email protected]
-----------------------------------------
-
Apr 8th, 2002, 08:37 PM
#2
Thread Starter
PowerPoster
Come on someone... Beaker?
-----------------------------------------
-RJ
[email protected]
-----------------------------------------
-
Apr 8th, 2002, 08:40 PM
#3
Thread Starter
PowerPoster
Ah bugger it - I got it... 
VB Code:
strSQLQuery = "SELECT MAX(AnswerCount) FROM " _
& "(SELECT questionNumber, COUNT(questionNumber) AS " _
& "AnswerCount FROM [Answers] GROUP BY questionNumber)"
-----------------------------------------
-RJ
[email protected]
-----------------------------------------
-
Apr 8th, 2002, 08:44 PM
#4
PowerPoster
Well i was typing a reply and saw you'd got it!!
Hey i it's only 9:30am here!
-
Apr 8th, 2002, 08:46 PM
#5
Thread Starter
PowerPoster
Lucky you... I 've been at work for nearly 4 hours.... 
Then it's off to Uni at 1 for another 3 hours.... 
OK, here's one then - same problem, but I only want to check a limited set of questionNumber which are in an array... any good ways to do it?
-----------------------------------------
-RJ
[email protected]
-----------------------------------------
-
Apr 8th, 2002, 08:53 PM
#6
-
Apr 8th, 2002, 09:16 PM
#7
Thread Starter
PowerPoster
The latter, I'll try and give you a quick overview. I have 2 tables - one for [answers], one for [questions]. [answers] may have 3 entries for question 1, 5 for question 2, 10 for question 3.
I need to know the highest number of answers - in this case it is 10. But say I am only asking question 1 & 2 - I only need the maximum from those 2 questions, which is 5.
So I need a query to tell me the maximum number of answers for a specific set of questions (1 & 2 in the example above above). The possible range is a list of numbers in an array.
A separate question also - how do I modify the working query I posted to check 2 different tables, with effectively the same structure. (One has additional fields, but these are not mentioned in the query).
-----------------------------------------
-RJ
[email protected]
-----------------------------------------
-
Apr 8th, 2002, 09:56 PM
#8
PowerPoster
1) How are you opening to get that information?
I.e cant you just use
VB Code:
strSQLQuery = "SELECT questionnumber FROM tablename"
Shouldnt that return all options anyway?
If you want a count you did it in the one u posted b4!
2) Check out UNION should help you!!
example:
VB Code:
Select name from table1 where name="rj"
UNION
select whatname from table2 where whatname="rj"
-
Apr 8th, 2002, 10:01 PM
#9
Thread Starter
PowerPoster
I can get a count OK, but if I want to only draw that count from a select portion of the records, what do I do...
shall look at UNION - probably what I want.
-----------------------------------------
-RJ
[email protected]
-----------------------------------------
-
Apr 8th, 2002, 10:10 PM
#10
Thread Starter
PowerPoster
Wanna help me out with this UNION query?
VB Code:
strSQLQuery = "SELECT MAX(AnswerCount) FROM (" _
& "SELECT MAX(AnswerCount) FROM " _
& "(SELECT questionNumber, COUNT(questionNumber) AS AnswerCount " _
& "FROM [Answers] GROUP BY questionNumber) " _
& "UNION " _
& "SELECT MAX(AnswerCount) FROM " _
& "(SELECT questionNumber, COUNT(questionNumber) AS AnswerCount " _
& "FROM [MultipleChoiceAnswers] GROUP BY questionNumber)" _
& ")"
It's not working. I had it executing correctly at some point, but it was only returning the MAX from [Answers] not the max from both tables.
The above query won't execute though...
-----------------------------------------
-RJ
[email protected]
-----------------------------------------
-
Apr 8th, 2002, 10:10 PM
#11
PowerPoster
You only want a count of certain records selected by a sql statement!
What statement are you using now?
Umm rs.recordcount perhaps??
later
b
-
Apr 8th, 2002, 10:19 PM
#12
Thread Starter
PowerPoster
It's all good. I got it now.
The UNION part, that is...
-----------------------------------------
-RJ
[email protected]
-----------------------------------------
-
Apr 8th, 2002, 10:24 PM
#13
PowerPoster
Sorry as i was replying the computer stuffed up so i went and played table tennis against one of the directors and had a beer to calm me down.
What about the maximum number of multiple choice answers?
This is for your course yes?
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|