【问题标题】:Calculate difference of column values between two row in Spark SQL计算 Spark SQL 中两行之间列值的差异
【发布时间】:2017-12-24 01:32:14
【问题描述】:

我有一个日志表,其中包含云存储数据上的 spark 实例中的如下客户数据。我正在尝试使用 apache zeppelin over spark 进行查询

CustomerID TimeStamp Distance
------------------------------
1           12.00        310
2           12.00        821
1           12.01        313
3           12.01        734
2           12.01        821
1           12.03        323
3           12.02        734
2           12.03        824

我想知道是否有任何客户在他们的两个连续行程中的距离超过 3 我尝试在客户 ID 的同一张表上进行连接,并将上述条件放在下面的 where 子句中,但这没有帮助;我认为 customerID 上的加入不正确,我得到了整个结果集

Select t1.customerID, t1.timestamp 
from sometable
inner join sometable t2 on t2.customerID = t1.customerID
where t2.timestamp-t1.timestamp < .02 and t2.distance - t1.distance > 3

【问题讨论】:

    标签: sql apache-spark apache-zeppelin


    【解决方案1】:

    您可以使用lag 来执行此操作。

    select customerID,timestamp
    from (select customerID,timestamp
          ,distance-lag(distance,1,distance) over(partition by customerID order by timestamp) as diff_with_prev_dist
          from sometable 
         ) t
    where diff_with_prev_dist > 3
    

    【讨论】:

    • 如果你能添加一个小解释,那就太好了。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多