【问题标题】:time difference between transaction of user用户交易时间差
【发布时间】:2019-11-23 11:49:59
【问题描述】:
Table: txn

customer_id | time_stamp 
-------------------------
1           | 00:01:03
1           | 00:02:04
2           | 00:03:05
2           | 00:04:06

想查询customer_id的每笔第一笔交易和下一笔交易的时间差

结果:

客户 ID |时差

1 | 61

选择客户 ID,... 来自txn

【问题讨论】:

  • 您使用的是 SQL Server 还是 Google BigQuery?这些是非常不同的数据库。
  • 请说明,使用什么DB,数据如何插入表中,如何获取?

标签: sql sql-server google-bigquery


【解决方案1】:

你想要lead()。 . .但众所周知,日期/时间函数是特定于数据库的。在 SQL Server 中:

select t.*,
       datediff(second,
                time_stamp,
                lead(time_stamp) over (partition by customer_id order by time_stamp)
               ) as diff_seconds
from t;

在 BigQuery 中:

select t.*,
       timestamp_diff(time_stamp,
                      lead(time_stamp) over (partition by customer_id order by time_stamp),
                       second
                     ) as diff_seconds
from t;

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2017-06-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-11-14
    • 2013-05-14
    相关资源
    最近更新 更多