Monday, March 2, 2015

Query to Get Grade History using Lag & Lead Function

SELECT CASE
          WHEN effective_start_date <>
                  LAG (effective_end_date, 1)
                     OVER (ORDER BY effective_end_date)
                  + 1
          THEN
             LAG (effective_end_date, 1) OVER (ORDER BY effective_end_date)
             + 1
          ELSE
             effective_start_date
       END
          date_from,
       effective_end_date date_to,
       (SELECT name
          FROM per_grades pg
         WHERE grade_id = prev)
          grade
  FROM (  SELECT effective_start_date,
                 effective_end_date,
                 grade_id prev,
                 LEAD (grade_id, 1, 0) OVER (ORDER BY effective_start_date) nex
            FROM per_all_assignments_f paaf
           WHERE assignment_number = '881'
        ORDER BY effective_start_date)

 WHERE prev <> nex

No comments:

Post a Comment