why don't group by work????

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • farid25
    New Member
    • Jun 2006
    • 11

    why don't group by work????

    there are three tables i want to query i use the following statement

    SELECT dbo.t_student_c ourse.StudentId ,dbo.t_books.bo ok_name
    FROM dbo.t_student_c ourse , dbo.t_students , dbo.t_books, dbo.t_groups
    WHERE dbo.t_books.boo k_id = dbo.t_student_c ourse.book_id
    AND dbo.t_books.boo k_id = dbo.t_student_c ourse.book_id AND dbo.t_student_c ourse.StudentId = dbo.t_students. Student_ID
    Group by dbo.t_student_c ourse.StudentId , dbo.t_books.boo k_name

    i got the following result

    Student_id book_name
    ========= ========
    4 City
    5 City
    3 Detective
    5 Detective
    1 PC
    3 PC


    when i use the following statement

    SELECT dbo.t_student_c ourse.StudentId
    FROM dbo.t_student_c ourse , dbo.t_students , dbo.t_books, dbo.t_groups
    WHERE dbo.t_books.boo k_id = dbo.t_student_c ourse.book_id
    AND dbo.t_books.boo k_id = dbo.t_student_c ourse.book_id AND dbo.t_student_c ourse.StudentId = dbo.t_students. Student_ID
    Group by dbo.t_student_c ourse.StudentId


    i got the following result


    Student_id
    =========
    1
    3
    4
    5

    i want to get the following reult

    Student_id book_name
    ========= ========
    4 City
    5 City
    3 Detective
    1 PC


    what i should do to get the prevoius result. why don't group by work????

    thanks for your help
  • Banfa
    Recognized Expert Expert
    • Feb 2006
    • 9067

    #2
    I do not think the group by clauses in either statement as you are not using an summary functions (like COUNT, SUM, AVG etc).

    In the second statement you removed dbo.t_books.boo k_name from the SELECT statement, try

    Code:
    SELECT dbo.t_student_course.StudentId, dbo.t_books.book_name
    FROM dbo.t_student_course , dbo.t_students , dbo.t_books, dbo.t_groups
    WHERE dbo.t_books.book_id = dbo.t_student_course.book_id
    AND dbo.t_books.book_id = dbo.t_student_course.book_id AND dbo.t_student_course.StudentId = dbo.t_students.Student_ID

    Comment

    Working...