如何从Prestodb中的前一行获取date_diff?

时间:2016-04-11 15:23:24

标签: sql presto

我试图从这个数据中获取Presto的diff_date

timespent | 2016-04-09T00:09:07.232Z | 1000          | general
timespent | 2016-04-09T00:09:17.217Z | 10000         | general
timespent | 2016-04-09T00:13:27.123Z | 250000        | general
timespent | 2016-04-09T00:44:21.166Z | 1144020654000 | general

这是我的查询

select _t, date_diff('second', from_iso8601_timestamp(_ts), SELECT from_iso8601_timestamp(f._ts) from logs f 
              where f._t = 'timespent'
               and f.dt = '2016-04-09'
               and f.uid = 'd2de01a1-8f78-49ce-a065-276c0c24661b'
               order by _ts)
from logs d
where _t = 'timespent'
and dt = '2016-04-09'
and uid = 'd2de01a1-8f78-49ce-a065-276c0c24661b'
order by _ts;

这是我得到的错误

Query 20160411_150853_00318_fmb4r failed: line 1:61: no viable alternative at input 'SELECT'

3 个答案:

答案 0 :(得分:4)

我想你想要lag()

select _t,
       date_diff('second', from_iso8601_timestamp(_ts),
                 lag(from_iso8601_timestamp(f._ts)) over (partition by uid order by dt)
                )
from logs d
where _t = 'timespent' and dt = '2016-04-09' and
      uid = 'd2de01a1-8f78-49ce-a065-276c0c24661b'
order by _ts;

答案 1 :(得分:1)

select date_diff('Day',from_iso8601_date(substr(od.order_date,1,10)),CURRENT_DATE) AS "diff_Days"
from order od;

答案 2 :(得分:0)

select date_diff('minute',timestamp '2010-05-22 16:00:00+0000', timestamp '2018-05-22 16:05:00+0000');