我需要它来返回每个城市每次更改的最大日期,所以基本上是他们每次访问城市的最后一天
我已尝试SELECT userid, city, Max(date) from table group by userid, city
,但它将单独的住宿与同一个城市合并为一个。
样本表
userid city date
------ --------- --------------
Francis Cambridge 06/15/2017
Francis Cambridge 07/01/2017
Francis London 07/02/2017
Francis Cambridge 07/03/2017
Francis NewYork 07/05/2017
Francis NewYork 07/20/2017
Francis NewYork 07/25/2017
Francis London 07/31/2017
所需的查询结果
userid city date
------ --------- --------------
Francis Cambridge 07/01/2017
Francis London 07/02/2017
Francis Cambridge 07/03/2017
Francis New York 07/25/2017
Francis London 07/31/2017
答案 0 :(得分:2)
使用Array
(
[0] => Array
(
[product_id] => 1
)
[1] => Array
(
[product_id] => 3
)
[2] => Array
(
[product_id] => 4
)
[3] => Array
(
[product_id] => 5
)
)
将上一行的用户ID和城市与当前行进行比较,并使用运行总和根据指定的条件分配组。然后你需要一个lag
来获取最新的日期。
group by
答案 1 :(得分:0)
所以,你想要留下没有后来留在给定城市的住宿
调用表stay_log:
select userid, city, date
from stay_log last_date
LEFT JOIN stay_log next_date ON
last_date.userid = next_date.userid and
last_date.city = next_date.city and
DateAdd(last_date, "1", 1) = next_date.date
WHERE next_date.userid is null