Home > database > Help!!!!!! Hive SQL for students enrolling consecutive times, twice to read time in half a year to c
Help!!!!!! Hive SQL for students enrolling consecutive times, twice to read time in half a year to c
Time:09-22
For example: student A has three records, three time respectively, 2017-1-1, 2017-3-1, 2017-10-1, Then straight to enrolling number 1; There are three records student B: three time respectively, 2017-1-1, 2017-3-1, 2017-8-30, So continuous to enrolling number is 2;
Please help!!!!!
CodePudding user response:
with t1 as ( Select the 'A' c1 and c2 from '2017-1-1' dual union all Select the 'A' c1 and c2 from '2017-10-1' dual union all Select the 'A' c1 and c2 from '2017-3-1' dual union all
Select 'B' c1, c2 from '2017-1-1' dual union all Select 'B' c1, c2 from '2017-3-1' dual union all Select 'B' c1, c2 from '2017-8-30' dual ) , t2 as ( The select row_number () over (order by c1, to_date (c2, '- dd yyyy - mm)) rn, c1, to_date (c2,' yyyy - mm - dd) c2 from t1 ) The select a.c 1, sum (case when 1 is not null and biggest months_between (biggest 2, 2) a.c & lt; 6 then 1 else=0 end) The from t2 a Left the join t2 on a.c b 1=1 and biggest a.r n=b.r n - 1 Group by a.c 1
CodePudding user response:
A bit strong , but there is a problem, that is for all newspaper reading times, and in a row, I'd like to ask this demand is the largest number of consecutive When five time is respectively 2017-1-1, In the 2017-3-1 s, In the 2017-10-1 s, In the 2017-11-1 s, In 2018-8-1, should be 2, not 3