让我想想,晚上回答你. 不过我只会用ORACLE PL/SQL

解决方案 »

  1.   

    select aa.lngID, DATEADD(day,1,aa.dtEnd) as datebegin,min(DATEADD(day,-1,bb.dtBegin)) as dateend
    from table1 aa,table1 bb
    where aa.lngID = bb.lngID and DATEADD(day,1,aa.dtEnd)<DATEADD(day,-1,bb.dtBegin)
    group by aa.lngID,DATEADD(day,1,aa.dtEnd)
      

  2.   

    leeyoong(莫西):
       有漏洞, 快成功了
      

  3.   

    表列名略有不同:
    SQL> select * from lend;        ID D1                   D2
    ---------- -------------------- ------------
             1 08/01/2001           09/11/2001
             1 10/01/2001           12/31/2001
             1 06/15/2002           08/31/2002
             2 01/01/2001           12/31/2001
             2 01/01/2002           03/31/2002
             
    --注意两个参数:统计开始日期:tjBegin 和统计结束日期 :tjEnd以下是SQL,是3个独立的语句 union 得到的:
    统计结果是日期从:tjBegin到:tjEnd期间的空闲时间段
    --****************************************************
    select id,:tjBegin,min(d1)-1
    from lend 
    group by id,:tjBegin      --统计有记录之前的空闲时间union allselect a.id,a.d2+1,b.d1-1
    from lend a,lend b
    where a.id=b.id
       and a.d2+1<b.d1 
       and b.d1=(select min(c.d1) from lend c where c.id=a.id and c.d1>a.d1)  --有记录期间的空闲union all   select id,MAX(d2)+1,:tjEnd
    from lend 
    group by id,'01/01/2003'
    having max(d2)+1<:tjEnd     --有记录以后的空闲
    --*******************************************************
      

  4.   

    更正:
    前半截也应该有个having min(d1)>:tjBegin才准确。
    中间忘了判断:tjBegin和:tjEnd
    后边应该是having max(d2)<:tjEnd    
    唉!错误不少
      

  5.   

    '01/01/2003'应该是:tjEnd
    我撞墙……
      

  6.   

    哦!xmxmlt(涛哥) 你说的漏洞是:没有下面两行吧
      1,    2002-09-01,  --
      2,    2002-04-01,  --
    这样吧,
    select aa.lngID, convert(varchar(10),DATEADD(day,1,aa.dtEnd),102) as datebegin,
    convert(varchar(10),min(DATEADD(day,-1,bb.dtBegin)),102) as dateend
    from table aa,table bb
    where aa.lngID = bb.lngID and DATEADD(day,1,aa.dtEnd)<DATEADD(day,-1,bb.dtBegin)
    group by aa.lngID,convert(varchar(10),DATEADD(day,1,aa.dtEnd),102)
    union
    select lngID,convert(varchar(10),max(DATEADD(day,1,dtEnd)),102),'--' from table group by lngID