我正在尝试将表连接到其自身的子集,但遇到了麻烦。我的输入包含一个 ID 和一个日期,如下所示:
2013-10-14 eca02319d91421445f82d570960c5d0c
2013-10-15 eca02319d91421445f82d570960c5d0c
2013-10-16 eca02319d91421445f82d570960c5d0c
2013-10-17 eca02319d91421445f82d570960c5d0c
2013-10-19 eca02319d91421445f82d570960c5d0c
2013-10-21 eca02319d91421445f82d570960c5d0c
我的目标是确定每个日期的相同 id 的最后一个先前日期,例如:
Date ID Last Date
2013-10-21 eca02319d91421445f82d570960c5d0c 2013-10-19
2013-10-19 eca02319d91421445f82d570960c5d0c 2013-10-17
etc.
使用以下 SQL,我得到以下输出:
select a.Day,a.entity, b.Day from Visitor a
LEFT OUTER JOIN Visitor b
on a.entity = b.entity
and b.day < a.day
where b.day is not null
output
2013-10-21 eca02319d91421445f82d570960c5d0c 2013-10-14
2013-10-21 eca02319d91421445f82d570960c5d0c 2013-10-15
2013-10-21 eca02319d91421445f82d570960c5d0c 2013-10-17
2013-10-21 eca02319d91421445f82d570960c5d0c 2013-10-19
2013-10-19 eca02319d91421445f82d570960c5d0c 2013-10-14
2013-10-19 eca02319d91421445f82d570960c5d0c 2013-10-15
为 a.day 的每个实例以及按降序处理 b.day 的每个实例只获取一条记录需要什么?
先感谢您!