Skip to content

Instantly share code, notes, and snippets.

@mburbea
Last active August 6, 2020 04:58
Show Gist options
  • Save mburbea/b8542b732a2108c3407eac1bbf993d04 to your computer and use it in GitHub Desktop.
Save mburbea/b8542b732a2108c3407eac1bbf993d04 to your computer and use it in GitHub Desktop.
;with transfers as (
select account_id = row_number() over (order by 1/0)%400,
transfer_date = eomonth(getutcdate(),-abs(binary_checksum(newid()))%13)
from string_split(space(8000),' ')
),last_12_months as (
select month_end = eomonth(getutcdate(),-n), n
from(values(1),(2),(3),(4),(5),(6),(7),(8),(9),(10),(11),(12))f(n)
)
select t.account_id
from transfers t
join last_12_months l
on eomonth(t.transfer_date) = month_end
group by account_id
having sum(distinct n) = 78
drop table if exists #accounts
drop table if exists #transfers
select account_id =-1 + row_number() over (order by 1/0)
into #accounts
from string_split(space(399),' ')
select account_id = row_number() over (order by 1/0)%400
,transfer_date = eomonth(getutcdate(),-abs(binary_checksum(newid()))%13)
into #transfers
from string_split(space(8000),' ')
;with rec as (
select account_id,n=1
from #accounts a
where exists (select 1 from #transfers t where a.account_id=t.account_id and eomonth(transfer_date)=eomonth(getutcdate(),-1))
union all
select account_id,n+1
from rec a
where exists (select 1 from #transfers t where a.account_id = t.account_id and eomonth(transfer_date) = eomonth(getutcdate(),-(n+1)))
and n < 13
)
select account_id from rec where n=12
;with last_12_months as (
select month_end = eomonth(getutcdate(),-n), n
from(values(1),(2),(3),(4),(5),(6),(7),(8),(9),(10),(11),(12))f(n)
)
select t.account_id
from #transfers t
join last_12_months l
on eomonth(t.transfer_date) = month_end
group by account_id
having sum(distinct n) = 78
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment