我试图弄清楚presto返回以下结果的原因。
我有以下4个日期的表格:
pickup_week (date)
'2019-10-28'
'2019-11-04'
'2019-11-06'
'2019-12-16'
当我尝试这个简单的自我加入:
select
w1.pickup_week as pu_w1,
w2.pickup_week as pu_w2
from week_user_list w1
join week_user_list w2 on w1.pickup_week = w2.pickup_week + interval '7' day
order by w1.pickup_week
我得到以下结果:
w1.pickup_week w2.pickup_week
2019-11-04 2019-10-28
2019-11-11 2019-11-04
正如你所看到的,加入条件是:
w1.pickup_week = w2.pickup_week + interval '7' day
但我在返回表中看到的情况正好相反,w2.pickup_week是前一周,而不是条件中所述的后一周。
在这种情况下,我想要的输出是:
w1.pickup_week w2.pickup_week
2019-10-28 2019-11-04
2019-11-04 2019-11-11
我理解将条件恢复为
w1.pickup_week = w2.pickup_week - interval '7' day
会提供所需的输出,但我很困惑为什么以及为什么presto会返回这些类型的结果,条件声明对我来说似乎不直观。
这里发生了什么,正确的思考方式是什么?
谢谢!
您只需要将逻辑向后。使用-
:
select w1.pickup_week as pu_w1, w2.pickup_week as pu_w2
from week_user_list w1 join
week_user_list w2
on w1.pickup_week = w2.pickup_week - interval '7' day
order by w1.pickup_week;
这清楚地说明了w1
值在w2
值前一周为。我想你只是对日期/时间和日期算术的排序有点困惑。