Home > database >  Continuous date for an interval
Continuous date for an interval

Time:11-17

Are there any friends know how to implement the

CodePudding user response:

 with gpdata as 
(select 1 xuhao, 'shares' gp, to_date (' 20200301', 'yyyyMMdd') rq
The from dual
Union all
Select 2 xuhao, 'shares' gp, to_date (' 20200302', 'yyyyMMdd') rq
The from dual
Union all
Select 3 xuhao, 'shares' gp, to_date (' 20200303', 'yyyyMMdd') rq
The from dual
Union all
Select 4 xuhao, 'shares' gp, to_date (' 20200305', 'yyyyMMdd') rq
The from dual
Union all
Select 5 xuhao, 'shares' gp, to_date (' 20200306', 'yyyyMMdd') rq
The from dual
Union all
Select 6 xuhao, 'shares' gp, to_date (' 20200309', 'yyyyMMdd') rq
The from dual
Union all
Select 7 xuhao, 'B' gp, to_date (' 20200302 ', 'yyyyMMdd') rq
The from dual
Union all
Select eight xuhao, 'B' gp, to_date (' 20200303 ', 'yyyyMMdd') rq
The from dual
Union all
Select 9 xuhao, 'B' gp, to_date (' 20200304 ', 'yyyyMMdd') rq
The from dual
Union all
Select 10 xuhao, 'B' gp, to_date (' 20200305 ', 'yyyyMMdd') rq
The from dual
Union all
Select 11 xuhao, 'B' gp, to_date (' 20200308 ', 'yyyyMMdd') rq
The from dual
Union all
Select 12 xuhao, 'B' gp, to_date (' 20200309 ', 'yyyyMMdd') rq
The from dual
Union all
Select 13 xuhao, 'B' gp, to_date (' 20200311 ', 'yyyyMMdd') rq
The from dual)
Select gp,
To_char (min (rq), 'yyyyMMdd') start date,
To_char (Max (rq), 'yyyyMMdd') end date,
Max (rq) - min (rq) + 1 last days
The from gpdata t
Start with the not the exists (select 1
The from gpdata
Where gp=t.g p
And rq=t.r q - 1)
Connect by the prior gp=gp
And the prior rq=rq - 1
Group by gp, connect_by_root rq
Having a count (1) & gt; 0
The order by 1, 2;
  • Related