Home Forums SQL Server 2008 T-SQL (SS2K8) compare the next row with the previous row of same table RE: compare the next row with the previous row of same table

  • abhas (9/16/2014)


    Hi bros,

    I need to compare the next row with the previous row of same table and produce nonidentical column.

    for eg... say

    mytable has

    Row 1 => 1001 Abhas 120 150 180

    Row 2 => 1001 Abhas 150 150 180

    then my output would be as below:

    StudId Name fee1 fee2 fee3

    1001 120

    1001 Abhas 150 150 150

    i.e in first row of resultset, i want to show only those values which are changed alongwith studID and next row should display all values.

    Plz suggest me...

    Thanks

    Abhas.

    The concept of "previous row" in SQL server is invalid if you're relying on perceived natural order. There has to be a column that contains a date, IDENTITY, or something that identifies the order of the rows.

    If you'd like a code answer, please help us help you and read the article at the first link under helpful links in my signature line below. Thanks.

    --Jeff Moden


    RBAR is pronounced "ree-bar" and is a "Modenism" for Row-By-Agonizing-Row.
    First step towards the paradigm shift of writing Set Based code:
    ________Stop thinking about what you want to do to a ROW... think, instead, of what you want to do to a COLUMN.

    Change is inevitable... Change for the better is not.


    Helpful Links:
    How to post code problems
    How to Post Performance Problems
    Create a Tally Function (fnTally)