zoukankan      html  css  js  c++  java
  • SQL解决时间段重复合并问题

    合并时间段:

    将连续或者重叠的时间段合并。
    create table timesheets (
    task_id char(10) not null primary key,
    startdate date not null,
    enddate date not null,
    check(startdate<=enddate))
     
    insert into timesheets values
    (1,'1997-01-01','1997-01-03'),
    (2,'1997-01-02','1997-01-04'),
    (3,'1997-01-04','1997-01-05'),
    (4,'1997-01-06','1997-01-09'),
    (5,'1997-01-09','1997-01-09'),
    (6,'1997-01-09','1997-01-09'),
    (7,'1997-01-12','1997-01-15'),
    (8,'1997-01-13','1997-01-14'),
    (9,'1997-01-14','1997-01-14'),
    (10,'1997-01-17','1997-01-17')
     
    select  t1.startdate,max(t2.enddate)  from timesheets  as t1,timesheets t2 where t1.startdate<=t2.enddate 
    and not exists (
    select * from timesheets as t3,timesheets t4 where
     t3.enddate<t4.startdate and 
     t3.startdate>=t1.startdate and
     t3.enddate<=t2.enddate and 
     t4.startdate>=t1.startdate and
     t4.enddate<=t2.enddate
     and not exists (
     select * from timesheets t5 where t5.startdate between t3.startdate and t3.enddate
     and t5.enddate between t4.startdate and t4.enddate
     )
    )
    group by t1.startdate having t1.startdate=min(t2.startdate)
  • 相关阅读:
    html_Dom
    html_javascript
    html_之css
    协程
    进程和线程的总结
    html_基础标签
    html_头部<meta>设置
    Python_queue单项队列
    Python_paramiko模块
    Python_多进程multiprocessing
  • 原文地址:https://www.cnblogs.com/vansky/p/8900151.html
Copyright © 2011-2022 走看看