Last active
March 21, 2019 03:27
-
-
Save staticor/d5a84cfa2ec5b0e648a9298c044edd65 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
create table temp.result | |
as | |
select uid, dtnum | |
from ( | |
select uid, rndiff, dtnum | |
, row_number() over ( partition by uid order by dtnum desc ) rk | |
from ( | |
select uid, rndiff, count(dt) dtnum | |
from ( | |
select uid | |
, dt | |
, row_number() over(partition by uid order by dt) rn | |
, datediff(dt, '2018-01-01') diffdays | |
, datediff(dt, '2018-01-01') - row_number() over(partition by uid order by dt) as rndiff | |
from ( | |
select uid, substr(date_, 1, 10) as dt | |
from A | |
where udt = '20181231' | |
and time > 10 | |
and date_ between '2016-10-01' and '2018-12-31' | |
group by uid, substr(date_, 1, 10) | |
) tmp | |
) result | |
group by uid, rndiff | |
) midresult | |
) finalresult | |
where rk = 1 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
create table temp.result | |
as | |
select uid, dtnum, startdt, enddt | |
from ( | |
select uid, rndiff, dtnum, startdt , enddt | |
, row_number() over ( partition by uid order by dtnum desc ) rk | |
from ( | |
select uid, rndiff, count(dt) dtnum | |
, min(dt) as startdt , max(dt) as enddt | |
from ( | |
select uid | |
, dt | |
, row_number() over(partition by uid order by dt) rn | |
, datediff(dt, '2016-10-01') diffdays | |
, datediff(dt, '2016-10-01') - row_number() over(partition by uid order by dt) as rndiff | |
from ( | |
select uid, substr(date_, 1, 10) as dt | |
from A | |
where udt = '20190319' | |
and time > 10 | |
and date_ between '2016-10-01' and '2019-03-19' | |
and uid = 123 | |
group by uid, substr(date_, 1, 10) | |
) tmp | |
) result | |
group by uid, rndiff | |
) midresult | |
) finalresult | |
where rk = 1 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment