Time Difference between query result rows in SQLite: How To?

后端 未结 1 496
难免孤独
难免孤独 2021-01-25 05:55

Consider the following reviews table contents:

CustomerName    ReviewDT
Doe,John        2011-06-20 10:13:24
Doe,John        2011-06-20 10:54:45
Doe,         


        
1条回答
  •  隐瞒了意图╮
    2021-01-25 06:31

    To compute the seconds elapsed from one ReviewDT row to the next:

     SELECT q.CustomerName, q.ReviewDT,
       strftime('%s',q.ReviewDT) 
       - strftime('%s',coalesce((select r.ReviewDT from Reviews as r
                           where r.CustomerName = q.CustomerName
                           and r.ReviewDT < q.ReviewDT
                           order by r.ReviewDT DESC limit 1), 
                           q.ReviewDT))
      FROM Reviews as q WHERE q.CustomerName NOT NULL 
      ORDER BY q.CustomerName ASC, q.ReviewDT ASC;
    

    To get the DT of each ReviewDT and its preceding CustomerName row:

    SELECT q.CustomerName, q.ReviewDT,
      coalesce((select r.ReviewDT from Reviews as r
                          where r.CustomerName = q.CustomerName
                          and r.ReviewDT < q.ReviewDT
                          order by r.ReviewDT DESC limit 1), 
                          q.ReviewDT)
     FROM Reviews as q WHERE q.CustomerName NOT NULL 
     ORDER BY q.CustomerName ASC, q.ReviewDT ASC;
    

    0 讨论(0)
提交回复
热议问题