我有下表,我想找出哪些客户在他/她的出发地点是另一位在他/她之前<=5分钟出发的客户的出发地点。
例如,我有:
DT Customer_Name Start_location End_location Trip_fare
2019-11-01 08:17:42 Jane A B $10
2019-11-01 08:18:02 Mary C A $7
2019-11-01 08:18:04 Tom B D $12
2019-11-01 08:20:11 Harry E C $20
2019-11-01 08:21:22 Alex D A $5
2019-11-01 08:24:30 Sally C B $8
这就是我想要的:
DT Customer_Name Start_location End_location
2019-11-01 08:17:42 Jane A B
2019-11-01 08:18:04 Tom B D (cause Tom's start_location = B = Jane's end_location and the time difference between the 2 trips is within 5 minutes)
2019-11-01 08:21:22 Alex D A
2019-11-01 08:20:11 Harry E C
2019-11-01 08:24:30 Sally C B
在这里,玛丽已经从列表中删除,因为她的起始位置='c',这不是简的结束位置,简在她之前旅行了<=5分钟。
我为这个看起来很混乱的问题道歉。如果你需要进一步的澄清,一定要告诉我!
非常感谢你的帮助!
2条答案
按热度按时间anauzrmj1#
我有下表,我想找出哪些客户在他/她的出发地点是另一位在他/她之前<=5分钟出发的客户的出发地点。
你对问题的描述表明
not exists
:然而,汤姆、亚历克斯和萨莉都被除名了。从你对这个问题的描述来看,我认为这是正确的。
这是一把小提琴。
1aaf6o9v2#
由于查询与来自同一个表的客户相关,因此需要一个自联接。也就是说,您将表与自身连接起来。
要区分表的一个“示例”和另一个示例,需要别名:
你需要加入条件,这就是你的两个客户的关系。在您的示例中,这非常简单:
但是,这个查询得到的结果略有不同。你能找出原因吗?
结果:
这个问题解释了5分钟的减法。