oracle订阅服务器,Oracle SQL Developer订阅服务器-创建交叉表

cross apply

select uh.subscriber_id, m.month,

case when trunc(uh.start_date, 'MM') <= m.month

and (uh.end_date is null or uh.end_date >= add_months(m.month, 1))

then 1 else 0 end as active

from upcall_history uh

cross apply (

select add_months(trunc(sysdate, 'MM'), - level) as month

from dual

connect by level <= 36

) m

order by uh.subscriber_id, m.month;

with upcall_history (subscriber_id, start_date, end_date) as (

select 1, date '2015-09-04', '2015-12-15' from dual

union all select 2, date '2017-12-04', '2018-05-15' from dual

)

SUBSCRIBER_ID MONTH ACTIVE

------------- ---------- ----------

1 2015-07-01 0

1 2015-08-01 0

1 2015-09-01 1

1 2015-10-01 1

1 2015-11-01 1

1 2015-12-01 0

1 2016-01-01 0

...

2 2017-11-01 0

2 2017-12-01 1

2 2018-01-01 1

2 2018-02-01 1

2 2018-03-01 1

2 2018-04-01 1

2 2018-05-01 0

2 2018-06-01 0

with m (month) as (

select add_months(trunc(sysdate, 'MM'), - level)

from dual

connect by level <= 36

),

i (subscriber_id) as (

select distinct subscriber_id

from upcall_history

)

select i.subscriber_id, m.month,

case when uh.subscriber_id is null then 0 else 1 end as active

from m

cross join i

left join upcall_history uh

on uh.subscriber_id = i.subscriber_id

and trunc(uh.start_date, 'MM') <= m.month

and (uh.end_date is null or uh.end_date >= add_months(m.month, 1))

order by i.subscriber_id, m.month;

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值