我有一个数据集,代表个人使用的形式,他们必须输入值。
表格可以在“名字”之外的任何顺序完成,即第1步。
时间戳用于表示表单的完成顺序。
例如 - 用户12345678于2017-10-25 04:58:08开始填写表格并按顺序填写表格。
用户12345679在同一天的05:00:02开始填写表单,但只到第2步
用户12345680在05:05:06开始填写表格,但是分散注意力并且没有超出第1步,但在完成之前又返回了两次
用户12345681在06:31:12开始填写表单并从步骤1开始,然后随机填写表单。
IdDialPlan Prefixe Type Country Description
----------- --------------- -------- --------------- ------------------------------
471 1646 Fixed United States United States - New York
32284 331 Fixed France France - Paris City
32318 3317044 Fixed France France - Paris City
34658 216 Fixed Tunisia Tunisia
34659 21620 Mobile Tunisia Tunisia - Mobile - Tunisiana
35571 3906 Fixed Italy Italy - Rome
我编写的代码如下所示,并引用了一个预先存在的表,其中CASE WHEN用于在名为'events'的变量中为表单的每个步骤分配一个数字:
| date | timestamp | user_id | visit_id | event | event_seq |
|--------------------------------------------------------------------------------------|
|2017-10-25| 2017-10-25 04:58:08| 12345678 | 1234 | firstname | 1 |
|2017-10-25| 2017-10-25 04:58:20| 12345678 | 1234 | lastname | 2 |
|2017-10-25| 2017-10-25 04:58:35| 12345678 | 1234 | dob | 3 |
|2017-10-25| 2017-10-25 04:58:40| 12345678 | 1234 | postcode | 4 |
|2017-10-25| 2017-10-25 04:58:40| 12345678 | 1234 | email | 5 |
|2017-10-25| 2017-10-25 05:00:02| 12345679 | 1235 | firstname | 1 |
|2017-10-25| 2017-10-25 05:00:10| 12345679 | 1235 | lastname | 2 |
|2017-10-25| 2017-10-25 05:05:06| 12345680 | 1236 | firstname | 1 |
|2017-10-25| 2017-10-25 05:30:24| 12345680 | 1236 | firstname | 1 |
|2017-10-25| 2017-10-25 06:17:24| 12345680 | 1236 | firstname | 1 |
|2017-10-25| 2017-10-25 06:20:30| 12345680 | 1236 | lastname | 2 |
|2017-10-25| 2017-10-25 06:20:45| 12345680 | 1236 | dob | 3 |
|2017-10-25| 2017-10-25 06:20:45| 12345680 | 1236 | postcode | 4 |
|2017-10-25| 2017-10-25 06:20:45| 12345680 | 1236 | email | 5 |
|2017-10-25| 2017-10-25 06:31:12| 12345681 | 1237 | firstname | 1 |
|2017-10-25| 2017-10-25 06:31:18| 12345681 | 1237 | email | 5 |
|2017-10-25| 2017-10-25 06:31:50| 12345681 | 1237 | lastname | 2 |
|2017-10-25| 2017-10-25 06:32:16| 12345681 | 1237 | postcode | 4 |
|2017-10-25| 2017-10-25 06:32:40| 12345681 | 1237 | dob | 3 |
正如预期的那样,这似乎是将用户12345680的所有交互列入一个字符串;
SELECT date
,time_stamp
,user_id
,visit_id
,collect_list(events) as event_seq
FROM my_table
GROUP BY date
,start_time
time_stamp
,user_id
,visit_id
但是,我想看到的是每个行都被序列中的第一个事件标记,类似于下面的结果集,其中每个用户12345680的重新启动都发生在不同的行上。
| date | timestamp | user_id | visit_id | event_seq |
|----------------------------------------------------------------------|
|2017-10-25| 2017-10-25 04:58:08| 12345678 | 1234 | 1,2,3,4,5 |
|2017-10-25| 2017-10-25 05:00:02| 12345679 | 1235 | 1,2 |
|2017-10-25| 2017-10-25 05:05:06| 12345680 | 1236 |1,1,1,2,3,4,5|
|2017-10-25| 2017-10-25 06:31:12| 12345681 | 1237 | 1,5,2,4,3, |
任何人都可以提供有关如何使用collect_list来达到我想要的结果集的任何指导吗?
答案 0 :(得分:0)
使用LEAD窗口函数根据event_seq列生成新列next_event_seq。这将为您提供另一列,每行包含下一个事件seq。现在在where子句中使用它来比较event_seq.Anytime,event_seq小于next_event_seq,那么这意味着它是序列的一部分,需要进行分组。
select date,time_stamp,user_id,visit_id,collect_list(events) as event_seq
from
( select *,lead(event_seq,1) over (order by date,timestamp,user_id,event_seq) as next_event_seq from my_table ) T
where event_seq < T.next_event_seq
group by date,time_stamp,user_id,visit_id
答案 1 :(得分:0)
从您的SQL中,您似乎在表中有一个名为start_time的列。假设您有一个,请参阅下面的解决方案
表
CREATE EXTERNAL TABLE my_table(
event_date DATE,
event_start_timestamp TIMESTAMP,
event_timestamp TIMESTAMP,
user_id STRING,
visit_id STRING,
event STRING,
event_seq STRING)
ROW FORMAT DELIMITED FIELDS TERMINATED BY '|'
STORED AS TEXTFILE;
数据强>
2017-10-25| 2017-10-25 04:58:08| 2017-10-25 04:58:08| 12345678 | 1234 | firstname | 1
2017-10-25| 2017-10-25 04:58:08| 2017-10-25 04:58:20| 12345678 | 1234 | lastname | 2
2017-10-25| 2017-10-25 04:58:08| 2017-10-25 04:58:35| 12345678 | 1234 | dob | 3
2017-10-25| 2017-10-25 04:58:08| 2017-10-25 04:58:40| 12345678 | 1234 | postcode | 4
2017-10-25| 2017-10-25 04:58:08| 2017-10-25 04:58:40| 12345678 | 1234 | email | 5
2017-10-25| 2017-10-25 05:00:02| 2017-10-25 05:00:02| 12345679 | 1235 | firstname | 1
2017-10-25| 2017-10-25 05:00:02| 2017-10-25 05:00:10| 12345679 | 1235 | lastname | 2
2017-10-25| 2017-10-25 05:05:06| 2017-10-25 05:05:06| 12345680 | 1236 | firstname | 1
2017-10-25| 2017-10-25 05:30:24| 2017-10-25 05:30:24| 12345680 | 1236 | firstname | 1
2017-10-25| 2017-10-25 06:17:24| 2017-10-25 06:17:24| 12345680 | 1236 | firstname | 1
2017-10-25| 2017-10-25 06:17:24| 2017-10-25 06:20:30| 12345680 | 1236 | lastname | 2
2017-10-25| 2017-10-25 06:17:24| 2017-10-25 06:20:45| 12345680 | 1236 | dob | 3
2017-10-25| 2017-10-25 06:17:24| 2017-10-25 06:20:45| 12345680 | 1236 | postcode | 4
2017-10-25| 2017-10-25 06:17:24| 2017-10-25 06:20:45| 12345680 | 1236 | email | 5
2017-10-25| 2017-10-25 06:31:12| 2017-10-25 06:31:12| 12345681 | 1237 | firstname | 1
2017-10-25| 2017-10-25 06:31:12| 2017-10-25 06:31:18| 12345681 | 1237 | email | 5
2017-10-25| 2017-10-25 06:31:12| 2017-10-25 06:31:50| 12345681 | 1237 | lastname | 2
2017-10-25| 2017-10-25 06:31:12| 2017-10-25 06:32:16| 12345681 | 1237 | postcode | 4
2017-10-25| 2017-10-25 06:31:12| 2017-10-25 06:32:40| 12345681 | 1237 | dob | 3
SQL查询
SELECT event_date,
user_id,
visit_id,
event_start_timestamp,
collect_list(event_seq)
FROM (SELECT event_date,
event_start_timestamp,
event_timestamp,
user_id,
visit_id,
event_seq
FROM my_table
SORT BY user_id, visit_id, event_start_timestamp, event_timestamp ASC) v
GROUP BY event_date, user_id, visit_id, event_start_timestamp ;
<强>输出强>
2017-10-25 12345678 1234 2017-10-25 04:58:08 [" 1 "," 2 "," 3 "," 4 "," 5 "]
2017-10-25 12345679 1235 2017-10-25 05:00:02 [" 1 "," 2 "]
2017-10-25 12345680 1236 2017-10-25 05:05:06 [" 1 "]
2017-10-25 12345680 1236 2017-10-25 05:30:24 [" 1 "]
2017-10-25 12345680 1236 2017-10-25 06:17:24 [" 1 "," 2 "," 3 "," 4 "," 5 "]
2017-10-25 12345681 1237 2017-10-25 06:31:12 [" 1 "," 5 "," 2 "," 4 "," 3 "]
如果有效,请告诉我们!
请不要使用列名作为DATE,TIMESTAMP等,它们是保留字:)