Home Forums SQL Server 7,2000 T-SQL help with rewriting query (performance optimization) RE: help with rewriting query (performance optimization)

  • Barring me having botched order here since I don't have anything to test against this should be pretty colse to what you are after.

    SELECT

     ST.student_id,

     ST.last_name + ',  ' + ST.first_name + '(' + ST.grade + ')' AS [Name],

     R.program_provider_activity_id, PPA.program_activity_name + '                                                    Currently Reg : ' + (CASE WHEN (R.registration_is_active = 1) THEN 'Yes' ELSE ' No' END) AS program_activity_name,

     R.program_id,

     SUM(datediff(hh, s.session_start_time, s.session_end_time)) TotalHours,

     COUNT(DISTINCT (session_start_time)) TotalDays,

     COUNT(DISTINCT (datepart(ww, session_start_time))) TotalWeeks

    FROM

     dbo.ProgramProviderActivity PPA

    INNER JOIN

     dbo.Registration R

    INNER JOIN

     dbo.Student ST

    ON

     ST.student_id = R.student_id

    ON

     PPA.program_provider_activity_id = R.program_provider_activity_id

    LEFT JOIN

     dbo.Attendance A

     INNER JOIN

      dbo.Session S

     ON

      S.program_provider_activity_id = R.program_provider_activity_id AND

      S.program_id = R.program_id AND

      S.Session_id = A.Session_id

    ON

     A.attendence_status = 1 AND

     A.student_id = ST.student_id

    GROUP BY

     ST.student_id,

     ST.last_name + ',  ' + ST.first_name + '(' + ST.grade + ')',

     R.program_provider_activity_id, PPA.program_activity_name + '                                                    Currently Reg : ' + (CASE WHEN (R.registration_is_active = 1) THEN 'Yes' ELSE ' No' END),

     R.program_id

    -- Noter may have to do some work still to get Order By right because of Group By

    ORDER BY

     ST.last_name + ' ' + ST.first_name,

     ST.grade