Thursday, 10 March 2011

Displaying current row and previous row

This query will display current row and its previous row.

For example:

Table->HBC

Hire_date               Batch_id            Status
07-Mar-2011          4                         2
08-Mar-2011          4                         3
09-Mar-2011          4                         4

Query:

SELECT    Hire_date,
                  Batch_id,
                  Status     Current_Status,
                  LAG(Status)  Over(order by Hire_date) Previous_Status
FROM      HBC
WHERE    Batch_id=4; 

Output:

Hire_date               Batch_id            Current_Status  Previous_Status
07-Mar-2011          4                         2                           
08-Mar-2011          4                         3                             2
09-Mar-2011          4                         4                             3


No comments:

Post a Comment