Results 1 to 13 of 13

Thread: SQL question...

  1. #1

    Thread Starter
    PowerPoster rjlohan's Avatar
    Join Date
    Sep 2001
    Location
    Sydney, Australia
    Posts
    3,205

    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]
    -----------------------------------------

  2. #2

    Thread Starter
    PowerPoster rjlohan's Avatar
    Join Date
    Sep 2001
    Location
    Sydney, Australia
    Posts
    3,205
    Come on someone... Beaker?
    -----------------------------------------
    -RJ
    [email protected]
    -----------------------------------------

  3. #3

    Thread Starter
    PowerPoster rjlohan's Avatar
    Join Date
    Sep 2001
    Location
    Sydney, Australia
    Posts
    3,205
    Ah bugger it - I got it...

    VB Code:
    1. strSQLQuery = "SELECT MAX(AnswerCount) FROM " _
    2.    & "(SELECT questionNumber, COUNT(questionNumber) AS " _
    3.    & "AnswerCount FROM [Answers] GROUP BY questionNumber)"
    -----------------------------------------
    -RJ
    [email protected]
    -----------------------------------------

  4. #4
    PowerPoster Beacon's Avatar
    Join Date
    Jan 2001
    Location
    Pub Floor
    Posts
    3,188
    Well i was typing a reply and saw you'd got it!!

    Hey i it's only 9:30am here!

  5. #5

    Thread Starter
    PowerPoster rjlohan's Avatar
    Join Date
    Sep 2001
    Location
    Sydney, Australia
    Posts
    3,205
    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]
    -----------------------------------------

  6. #6
    PowerPoster Beacon's Avatar
    Join Date
    Jan 2001
    Location
    Pub Floor
    Posts
    3,188
    Bit

    You want to perform a query on an array??

    Or

    you want to sue the array in the query?

    b

  7. #7

    Thread Starter
    PowerPoster rjlohan's Avatar
    Join Date
    Sep 2001
    Location
    Sydney, Australia
    Posts
    3,205
    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]
    -----------------------------------------

  8. #8
    PowerPoster Beacon's Avatar
    Join Date
    Jan 2001
    Location
    Pub Floor
    Posts
    3,188
    1) How are you opening to get that information?

    I.e cant you just use
    VB Code:
    1. 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:
    1. Select name from table1 where name="rj"
    2. UNION
    3. select whatname from table2 where whatname="rj"

  9. #9

    Thread Starter
    PowerPoster rjlohan's Avatar
    Join Date
    Sep 2001
    Location
    Sydney, Australia
    Posts
    3,205
    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]
    -----------------------------------------

  10. #10

    Thread Starter
    PowerPoster rjlohan's Avatar
    Join Date
    Sep 2001
    Location
    Sydney, Australia
    Posts
    3,205
    Wanna help me out with this UNION query?
    VB Code:
    1. strSQLQuery = "SELECT MAX(AnswerCount) FROM (" _
    2.         & "SELECT MAX(AnswerCount) FROM " _
    3.         & "(SELECT questionNumber, COUNT(questionNumber) AS AnswerCount " _
    4.         & "FROM [Answers] GROUP BY questionNumber) " _
    5.         & "UNION " _
    6.         & "SELECT MAX(AnswerCount) FROM " _
    7.         & "(SELECT questionNumber, COUNT(questionNumber) AS AnswerCount " _
    8.         & "FROM [MultipleChoiceAnswers] GROUP BY questionNumber)" _
    9.         & ")"

    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]
    -----------------------------------------

  11. #11
    PowerPoster Beacon's Avatar
    Join Date
    Jan 2001
    Location
    Pub Floor
    Posts
    3,188
    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

  12. #12

    Thread Starter
    PowerPoster rjlohan's Avatar
    Join Date
    Sep 2001
    Location
    Sydney, Australia
    Posts
    3,205
    It's all good. I got it now.
    The UNION part, that is...
    -----------------------------------------
    -RJ
    [email protected]
    -----------------------------------------

  13. #13
    PowerPoster Beacon's Avatar
    Join Date
    Jan 2001
    Location
    Pub Floor
    Posts
    3,188
    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
  •  



Click Here to Expand Forum to Full Width