相差超过一个小时的数据

需求

  
  
  1. create table   test_1 as
  2. select 'w' cid,'a' sid ,'2016-04-17 02:25:45' dt from dual
  3. union all
  4. select 'w' cid,'b','2016-04-17 02:31:46' from dual
  5. union all
  6. select 'w' cid,'c','2016-04-17 02:35:01' from dual
  7. union all
  8. select 'w' cid,'d','2016-04-17 02:47:56' from dual
  9. union all
  10. select 'w' cid,'e','2016-04-17 02:49:40' from dual
  11. union all
  12. select 'w' cid,'f','2016-04-17 02:52:25' from dual
  13. union all
  14. select 'w' cid,'g','2016-04-17 02:54:39' from dual
  15. union all
  16. select 'w' cid,'h','2016-04-17 18:22:45' from dual
  17. union all
  18. select 'w' cid,'i','2016-04-17 20:16:25' from dual
  19. union all
  20. select 'w' cid,'j','2016-04-18 01:15:18' from dual
  21. union all
  22. select 'w' cid,'k','2016-04-18 06:07:13' from dual
  23. union all
  24. select 'w' cid,'l','2016-04-18 16:02:22' from dual
  25. union all
  26. select 'w' cid,'m','2016-04-18 16:14:49' from dual
  27. union all
  28. select 'w' cid,'n','2016-04-18 16:16:41' from dual
  29. union all
  30. select 'w' cid,'o','2016-04-18 16:19:36' from dual
  31. union all
  32. select 'w' cid,'p','2016-04-18 16:52:20' from dual
  33. union all
  34. select 'w' cid,'q','2016-04-18 16:53:39' from dual
  35. union all
  36. select 'w' cid,'r','2016-04-18 17:24:17' from dual
  37. union all
  38. select 'w' cid,'s','2016-04-18 17:29:15' from dual
  39. union all
  40. select 'w' cid,'t','2016-04-18 20:11:54' from dual
  41. union all
  42. select 'w' cid,'u','2016-04-18 20:17:16' from dual
  43. union all
  44. select 'w' cid,'v','2016-04-18 20:25:39' from dual
  45. sid之间相差超过一个小时的数据
  46. 比如 a b 相差不超过一个小时则还是取第一个(2016/4/17 02:25:45
  47. a h 相差超过1个小时则取 h2016/4/17 18:22:45
  48. 后续的sid需要和 h 2016/4/17 18:22:45)这个sid的时间比较 。依次比较
  49. 最终输出结果为
  50. w,a,2016/4/17 02:25:45
  51. w,h,2016/4/17 18:22:45
  52. w,i,2016/4/17 20:16:25
  53. w,j,2016/4/18 01:15:18
  54. w,k,2016/4/18 06:07:13
  55. w,l,2016/4/18 16:02:22
  56. w,r,2016/4/18 17:24:17
  57. w,t,2016/4/18 20:11:54

教主

 
 
  1. with v1 as
  2. (select CID,
  3.         SID,
  4.         DT,
  5.         min(dt) over(partition by cid order by dt range between 1 / 24 following and unbounded following) as after_hour,
  6.         min(dt) over(partition by cid) as min_dt
  7.    from test_1 a)
  8. select * from v1
  9. start with dt = min_dt
  10. connect by dt = (prior after_hour)  

ITPUB

 
 
  1. select  cid,
  2.         sid,
  3.         dt
  4. from
  5. (select cid,
  6.         sid,
  7.         to_char(dt, 'yyyy-mm-dd hh24:mi:ss') dt,
  8.         count(*) over(partition by cid order by dt range between current row and interval '1' hour following) cnt,
  9.         row_number() over(partition by cid order by dt) rn
  10.     from test_1)
  11.  start with rn = 1
  12.  connect by prior rn + prior cnt = rn
  13.  ;
  14. 学习12C新功能:
  15. SELECT cid,sid,dt
  16. FROM test_1
  17. MATCH_RECOGNIZE (
  18.     PARTITION BY cid
  19.     ORDER BY dt
  20.     MEASURES CLASSIFIER() as FLAG
  21.     ALL ROWS PER MATCH
  22.     PATTERN ((A|B)+)
  23.     DEFINE
  24.        A as dt>last(A.DT,1)+1/24 or prev(dt) is null
  25.     )
  26. WHERE FLAG='A'
  27. ;


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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值