Results 1 to 3 of 3
  1. #1
    Join Date
    Jun 2008
    Posts
    1

    Unanswered: MS-SQL server 2005

    I wanted to calculate the difference between the current row and the previous row in MS-SQL server 2005

    For eg.,

    I wanted to calculate the difference and the output as in last column

    prd year |fixes | increase

    ------+------+---------
    a 2006 1 0
    a 2007 4 3
    a 2008 3 -1
    b 2006 7 0
    b 2007 8 1
    b 2008 9 1
    Can you please help me with the query on this

  2. #2
    Join Date
    Jun 2005
    Posts
    319
    GOOGLE ROWNUMBER() you should be able to figure it out with a little testing

  3. #3
    Join Date
    Feb 2003
    Location
    India
    Posts
    216
    Code:
    select prd,yr,fixes, 
    isnull((select top 1 a.fixes - fixes from mytable b where b.prd=a.prd and b.yr < a.yr order by yr desc),0)
    from mytable a

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •