我有一个如下所示的数据集:
respondent_id day_session daydiff
nmo876 11/19/2017 0
nmo876 11/20/2017 1
nmo876 11/21/2017 1
nmo876 11/23/2017 2
nmo876 11/24/2017 1
nmo876 11/25/2017 1
nmo876 11/26/2017 1
nmo876 11/27/2017 1
nmo876 11/28/2017 1
nmo876 11/29/2017 1
nmo876 11/30/2017 1
nmo876 12/1/2017 1
nmo876 12/2/2017 1
nmo876 12/3/2017 1
nmo876 12/4/2017 1
nmo876 12/5/2017 1
nmo876 12/6/2017 1
nmo876 12/7/2017 1
nmo876 12/8/2017 1
nmo876 12/9/2017 1
nmo876 12/10/2017 1
nmo876 12/11/2017 1
nmo876 12/12/2017 1
nmo876 12/13/2017 1
nmo876 12/14/2017 1
nmo876 12/15/2017 1
nmo876 12/16/2017 1
nmo876 12/17/2017 1
nmo876 12/18/2017 1
nmo876 12/19/2017 1
nmo876 12/20/2017 1
nmo876 12/23/2017 3
nmo876 12/24/2017 1
nmo876 12/26/2017 2
nmo876 12/27/2017 1
nmo876 12/28/2017 1
nmo876 12/29/2017 1
nmo876 12/30/2017 1
nmo876 12/31/2017 1
nmo876 1/2/2018 2
nmo876 1/3/2018 1
nmo876 1/4/2018 1
nmo876 1/5/2018 1
我想编写一个脚本,从用户可能有多个连续day_sessions的数据集中选择最大的连续day_sessions块,即daydiff = 1.对于nmo876,输出为27。
以下是代码应该计算连续每日会话的最大块的更多数据:
jkl567 11/19/2017 1
jkl567 11/20/2017 1
jkl567 11/21/2017 1
jkl567 11/22/2017 1
jkl567 11/23/2017 1
jkl567 11/24/2017 1
jkl567 11/25/2017 1
jkl567 11/26/2017 1
jkl567 11/27/2017 1
jkl567 11/28/2017 1
jkl567 11/29/2017 1
jkl567 11/30/2017 1
jkl567 12/1/2017 1
jkl567 12/2/2017 1
jkl567 12/3/2017 1
jkl567 12/4/2017 1
jkl567 12/5/2017 1
jkl567 12/6/2017 1
jkl567 12/7/2017 1
jkl567 12/8/2017 1
jkl567 12/9/2017 1
jkl567 12/10/2017 1
jkl567 12/11/2017 1
jkl567 12/12/2017 1
jkl567 12/13/2017 1
jkl567 12/14/2017 1
jkl567 12/15/2017 1
jkl567 12/16/2017 1
jkl567 12/17/2017 1
jkl567 12/18/2017 1
jkl567 12/19/2017 1
jkl567 12/20/2017 1
jkl567 12/21/2017 1
jkl567 12/22/2017 1
jkl567 12/23/2017 1
jkl567 12/24/2017 1
jkl567 12/25/2017 1
jkl567 12/26/2017 2
jkl567 12/28/2017 1
jkl567 12/29/2017 3
jkl567 1/1/2018 1
jkl567 1/2/2018 1
jkl567 1/3/2018 1
jkl567 1/4/2018 1
答案 0 :(得分:3)
您可以使用变量来实现此目的。
SQL> with test (fullname) as
2 (select 'Metro Goldwyn Mayer' from dual
3 )
4 select substr(fullname, 1, instr(fullname, ' ', 1, 1) - 1) first_name,
5 substr(fullname,
6 instr(fullname, ' ', 1, 1) + 1,
7 instr(fullname, ' ', 1, 2) - instr(fullname, ' ', 1, 1) - 1) middle_name,
8 substr(fullname,
9 instr(fullname, ' ', 1, 2) + 1) last_name
10 from test;
FIRST_NAME MIDDLE_NAME LAST_NAME
-------------------- -------------------- --------------------
Metro Goldwyn Mayer
SQL>
<强> 解释 强>
内部查询使用附加列作为计数器准备结果,仅当daydiff为1时才会递增。
外部查询然后合并结果并显示最高值。