CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 3 of 3

Thread: SQL SubQueries

  1. #1
    Join Date
    Mar 2001
    Location
    County Durham, England
    Posts
    238

    SQL SubQueries

    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.

  2. #2
    Join Date
    Aug 2000
    Location
    Namibia
    Posts
    139

    Re: SQL SubQueries

    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.


  3. #3
    Join Date
    May 2001
    Location
    Canada
    Posts
    182

    Re: SQL SubQueries

    =====
    SELECT TestNumber, QuestionNo, AnsweredType, count(*) CountAnsweredType
    FROM TestAnswers
    WHERE TestNumber=1644
    GROUP BY TestNumber, QuestionNo,AnsweredType
    ====

    Regards,

    Michi

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

Featured