SQL/AWS Athena 和 AWS Quicksight 上的差异分组行
Diff grouped rows on SQL / AWS Athena and AWS Quicksight
我有关于用户跟踪的数据(用户和会话未排序):
user session cummulative_time_spent
A 1 2
A 1 5
A 1 10
A 2 3
B 5 1
B 5 200
B 6 5
B 6 6
我想创建花费的时间来区分按会话分组的行。我的预期结果是这样的:
user session cummulative_time_spent duration
A 1 2 2
A 1 5 3
A 1 10 5
A 2 3 3
B 5 1 1
B 5 200 199
B 6 5 5
B 6 6 1
我不知道如何在 SQL (AWS Athena) 或 Quicksight 上执行此操作。
您可以使用 lag()
:
select
t.*,
cumulative_time_spent
- coalesce(
lag(cumulative_time_spent)
over(partition by user, session order by cumulative_time_spent),
0
) duration
from mytable
正如 GMB 指出的那样,您想要的功能是 lag()
。但是,它有一个简化计算的三参数形式:
select t.*,
(cumulative_time_spent -
lag(cumulative_time_spent, 1, cumulative_time_spent) over
(partition by user, session order by cumulative_time_spent)
) as duration
from mytable
我有关于用户跟踪的数据(用户和会话未排序):
user session cummulative_time_spent
A 1 2
A 1 5
A 1 10
A 2 3
B 5 1
B 5 200
B 6 5
B 6 6
我想创建花费的时间来区分按会话分组的行。我的预期结果是这样的:
user session cummulative_time_spent duration
A 1 2 2
A 1 5 3
A 1 10 5
A 2 3 3
B 5 1 1
B 5 200 199
B 6 5 5
B 6 6 1
我不知道如何在 SQL (AWS Athena) 或 Quicksight 上执行此操作。
您可以使用 lag()
:
select
t.*,
cumulative_time_spent
- coalesce(
lag(cumulative_time_spent)
over(partition by user, session order by cumulative_time_spent),
0
) duration
from mytable
正如 GMB 指出的那样,您想要的功能是 lag()
。但是,它有一个简化计算的三参数形式:
select t.*,
(cumulative_time_spent -
lag(cumulative_time_spent, 1, cumulative_time_spent) over
(partition by user, session order by cumulative_time_spent)
) as duration
from mytable