Results 1 to 2 of 2
  1. #1
    Join Date
    Dec 2012
    Posts
    1

    Unanswered: Select value from row above/after

    Hi,

    I have a table reports like the following

    Date ID Sum
    10/3/2010 1 6
    11/3/2010 1 5
    12/3/2010 1 8
    16/3/2010 1 5
    20/3/2010 1 12

    The user would select a specific date and would like to know the value in the sum field several rows before (it could be any number of rows before).
    The dates are not consecutive, as seen in the table.

    for example. I can select 16/3/2010 and 2 rows before. The return value would be 5, as this is the sum column value at date 11/3/2010.

    Any ideas?

    Thanks,
    T.

  2. #2
    Join Date
    Nov 2003
    Posts
    2,934
    Provided Answers: 12
    This is easily possible with SQL Server 2012 (using the lag() or lead() function).

    Which version are you using?
    I will not read nor answer questions where the SQL code is messy and not formatted properly using [code] tags: http://www.dbforums.com/misc.php?do=bbcode#code

    Tips for good questions:

    http://tkyte.blogspot.de/2005/06/how...questions.html
    http://wiki.postgresql.org/wiki/SlowQueryQuestions
    http://catb.org/esr/faqs/smart-questions.html

Posting Permissions

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