Select 按名称、jobId 和 Action 分组的时间戳的最大值
Select max value of timestamp grouped by name, jobId and Action
我有我的 table 如下图所示(在图像的上部),我想 select max timestamp
[=14= 执行的工作].例如,User1
执行了许多 Process1
活动,我们需要 select 最新的 activity,即 Start
Process1
在 5.11.2020
.
我不能使用 group by
,因为我必须在 select
和 group by
中包含所有列名,因为时间戳是唯一的员工。
我怎样才能做到这一点?
示例数据:
| User | Activity | Timestamp | Action |
|-------|----------|-----------------|--------|
| User1 | process1 | 1.11.2020 10:00 | Start |
| User1 | process1 | 1.11.2020 10:30 | Stop |
| User1 | process1 | 1.11.2020 11:00 | Start |
| User1 | process1 | 1.11.2020 11:30 | Start |
| User1 | process1 | 5.11.2020 10:00 | Start |
| User2 | process1 | 5.11.2020 10:05 | Start |
| User2 | process1 | 5.11.2020 10:30 | Stop |
| User2 | process2 | 5.11.2020 10:45 | Start |
输出应该是:
| User | Activity | LastAction |
|-------|----------|------------|
| User1 | process1 | Start |
| User2 | process1 | Stop |
| User2 | process2 | Start |
您可以使用 row_number()
来识别每个用户的最新行和 activity:
select *
from (
select t.*,
row_number() over(partition by user, activity order by timestamp desc) rn
from mytable t
) t
where rn = 1
我有我的 table 如下图所示(在图像的上部),我想 select max timestamp
[=14= 执行的工作].例如,User1
执行了许多 Process1
活动,我们需要 select 最新的 activity,即 Start
Process1
在 5.11.2020
.
我不能使用 group by
,因为我必须在 select
和 group by
中包含所有列名,因为时间戳是唯一的员工。
我怎样才能做到这一点?
示例数据:
| User | Activity | Timestamp | Action |
|-------|----------|-----------------|--------|
| User1 | process1 | 1.11.2020 10:00 | Start |
| User1 | process1 | 1.11.2020 10:30 | Stop |
| User1 | process1 | 1.11.2020 11:00 | Start |
| User1 | process1 | 1.11.2020 11:30 | Start |
| User1 | process1 | 5.11.2020 10:00 | Start |
| User2 | process1 | 5.11.2020 10:05 | Start |
| User2 | process1 | 5.11.2020 10:30 | Stop |
| User2 | process2 | 5.11.2020 10:45 | Start |
输出应该是:
| User | Activity | LastAction |
|-------|----------|------------|
| User1 | process1 | Start |
| User2 | process1 | Stop |
| User2 | process2 | Start |
您可以使用 row_number()
来识别每个用户的最新行和 activity:
select *
from (
select t.*,
row_number() over(partition by user, activity order by timestamp desc) rn
from mytable t
) t
where rn = 1