Results 1 to 4 of 4
  1. #1
    Join Date
    May 2010
    Posts
    2

    Unanswered: SQL compare previous row

    Dear All,

    I need help in writing a query and would appreciate any solution that would solve my problem Some background information:

    I have a table which records the price of a commodity at a given date and time (5 minute increment to be exact). The first column represents the Date. Second column represent the time and the third column is the price. The time span for this table is for a whole year.

    An extract of the table is as follow:

    1999/1/5 09:05 6430
    1999/1/5 09:10 6355
    1999/1/5 09:15 6365
    1999/1/5 09:20 6354
    1999/1/5 09:25 6321


    next day
    1999/1/6 09:05 6011
    1999/1/6 09:10 6050
    1999/1/6 09:15 6093
    1999/1/6 09:20 6080
    1999/1/6 09:25 6065


    to last day of the year
    1999/12/28 09:05 8560
    1999/12/28 09:10 8520
    1999/12/28 09:15 8500
    1999/12/28 09:20 8500
    1999/12/28 09:25 8505

    Here is my question :

    The query I would like to write is to find the days in which the price at 9:15 is higher than the price at 9:10 and the price at 9:10 is also higher than the price at 9:05!

    Love to hear from you~

  2. #2
    Join Date
    Jan 2007
    Location
    UK
    Posts
    11,434
    Provided Answers: 10
    What version of SQL Server are you using?
    George
    Home | Blog

  3. #3
    Join Date
    May 2010
    Posts
    2
    Dear George,

    I can use either SQL Server 2000 or 2008.


  4. #4
    Join Date
    Jan 2007
    Location
    UK
    Posts
    11,434
    Provided Answers: 10
    I'm sure this isn't the greatest solution ever, but hey...
    This is written using features that aren't available in SQL2000
    Code:
    DECLARE @your_table table (
       the_date date
     , the_time time
     , price    money
    )
    
    INSERT INTO @your_table (the_date, the_time, price)
      VALUES ('1999/1/5'  , '09:05', 6430)
           , ('1999/1/5'  , '09:10', 6355)
           , ('1999/1/5'  , '09:15', 6365)
           , ('1999/1/5'  , '09:20', 6354)
           , ('1999/1/5'  , '09:25', 6321)
           -- next day
           , ('1999/1/6'  , '09:05', 6011)
           , ('1999/1/6'  , '09:10', 6050)
           , ('1999/1/6'  , '09:15', 6093)
           , ('1999/1/6'  , '09:20', 6080)
           , ('1999/1/6'  , '09:25', 6065)
           -- to last day of the year
           , ('1999/12/28', '09:05', 8560)
           , ('1999/12/28', '09:10', 8520)
           , ('1999/12/28', '09:15', 8500)
           , ('1999/12/28', '09:20', 8500)
           , ('1999/12/28', '09:25', 8505)
    
    ; WITH cte AS (
      SELECT the_date
           , the_time
           , the_datetime
           , price
           , Row_Number() OVER (ORDER BY the_datetime ASC) As row_num
      FROM   (
              SELECT the_date
                   , the_time
                   , the_date + Convert(datetime, the_time) As the_datetime
                   , price
              FROM   @your_table
             ) As x
    )
    SELECT a.*
    FROM   cte As a
     LEFT
      JOIN cte As b
        ON b.row_num = a.row_num - 1
     LEFT
      JOIN cte As c
        ON c.row_num = b.row_num - 1
    WHERE  a.the_time = '09:05'
    AND    a.price < b.price
    AND    b.price < c.price
    George
    Home | Blog

Tags for this Thread

Posting Permissions

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