Self Join Table, MS SQL 2005

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • bumblebee
    New Member
    • Oct 2008
    • 3

    Self Join Table, MS SQL 2005

    Hi

    I have stucked in this query for quite some time.
    My table as follows

    StudentID Result ResultRating LastUpdate
    ------------------------------------------------------------------------
    StudentA 60 Average 6 June 2008
    StudentA 80 Good 20 June 2008
    StudentB 10 Bad 6 June 2008
    StudentB 50 Average 20 June 2008
    StudentB 100 Good 08 July 2008
    StudentC 100 Good 08 July 2008
    StudentB 50 Average 10 July 2008
    ------------------------------------------------------------------------

    How can I get the total number of student attaining "Good", "Average", "Bad" in each month, assuming we will only take the highest rating for each student. For example, June StudentA, will be a "Good" Student.

    The result will be as follows
    Number of Students ResultRating Month
    ------------------------------------------------------------------------
    1 Good June
    1 Average June
    0 Bad June
    2 Good July
    0 Average July
    0 Bad July
    ------------------------------------------------------------------------

    Hope someone could help .. Thank you in advanced.
  • ck9663
    Recognized Expert Specialist
    • Jun 2007
    • 2878

    #2
    Sounds like an assignment.

    Post what you have so far.

    -- CK

    Comment

    • bumblebee
      New Member
      • Oct 2008
      • 3

      #3
      I have came out with this sql

      SELECT MAX(Result) AS MaxResult, MONTH(LastUpdat ed) AS LastUpdatedMont h
      FROM dbo.StudentResu ltHistory
      GROUP BY StudentID, MONTH(LastUpdat ed)

      It did return the maximum score for each individual student for each month but it doesn't count the number of student in "Good" grades in each month.

      Comment

      • bumblebee
        New Member
        • Oct 2008
        • 3

        #4
        Hey I have solved it using 2 views, 1 stored procedure. Anyway, Thanks for your reply. ^^

        Comment

        Working...