【发布时间】:2023-07-31 19:05:01
【问题描述】:
我想为每个特定的 unit_of_measure 每个用户完成最后一个事件:
我有这张桌子:
person_id event_time event_derscription unit_of_measure
-----------------------------------------------------------------
1 20200801120101 "some description" "unit1"
1 20200801120501 "some description 2" "unit1"
1 20200801120501 "some description 2" "unit9"
2 20200801120301 "some description 3" "unit1"
2 20200801120501 "some description 4" "unit1"
预期输出是:
person_id event_time event_derscription unit_of_measure
-----------------------------------------------------------------
1 20200801120101 "some description" "unit1"
2 20200801120301 "some description 2" "unit1"
1 20200801120501 "some description 2" "unit9"
我尝试了什么:
select *
from
(select
person_id, event_time, event_derscription, unit_of_measure,
rank() over (partition by unit_of_measure order by event_time desc) as RN
from
test.person_events
where
partition_name = 20200801
group by
person_id, event_time, event_description, unit_of_measure)
where
RN = 1; // I try to use group by person_id to get the result for each person_id but it did not work
我上面代码的输出是:
person_id event_time event_derscription unit_of_measure
-----------------------------------------------------------------
2 20200801120301 "some description 2" "unit1"
1 20200801120501 "some description 2" "unit9"
我做错了什么吗?
【问题讨论】:
标签: sql oracle greatest-n-per-group presto trino