表结构
Serial_No D_DateTime        address
1         2012-6-13 20:00    1教室
2         2012-6-13 20:01    1教室
3         2012-6-13 20:04    2教室
4         2012-6-13 20:05    2教室
5         2012-6-13 20:06    2教室
6         2012-6-13 20:07    2教室
7         2012-6-13 20:08    1教室
8         2012-6-13 20:09    1教室
9         2012-6-13 20:10    2教室
10        2012-6-13 20:11    2教室
11        2012-6-13 20:12    2教室
12        2012-6-13 20:13    1教室
13        2012-6-13 20:14    1教室
表2
begin_dateteime  end_datetime      i_datetime
2012-6-13 20:04   2012-6-13 20:07    3分钟
2012-6-13 20:10   2012-6-13 20:12    2分钟
要得以下结果
 D_DateTime        address  i_datetime
2012-6-13 20:00    1教室
2012-6-13 20:01    1教室
2012-6-13 20:04    2教室
2012-6-13 20:07    2教室     3分钟
2012-6-13 20:08    1教室
2012-6-13 20:09    1教室
2012-6-13 20:10    2教室  
2012-6-13 20:12    2教室     2分钟
2012-6-13 20:13    1教室
2012-6-13 20:14    1教室查询表1一个时间段时 如果表2中有在这个时间段的记录就取这段时间的间隔分钟

解决方案 »

  1.   


    if OBJECT_ID('tb1','u') is not null drop table tb1 
    go
    if OBJECT_ID('tb2','u') is not null drop table tb2 
    go
    --Serial_No D_DateTime        address
    with tb1 as
    (
    select 1 as Serial_No,'2012-6-13 20:00' as D_DateTime,'1教室' as [address] union all
    select 2,'2012-6-13 20:01','1教室' union all
    select 3,'2012-6-13 20:04','2教室' union all
    select 4,'2012-6-13 20:05','2教室' union all
    select 5,'2012-6-13 20:06','2教室' union all
    select 6,'2012-6-13 20:07','2教室' union all
    select 7,'2012-6-13 20:08','1教室' union all
    select 8,'2012-6-13 20:09','1教室' union all
    select 9,'2012-6-13 20:10','2教室' union all
    select 10,'2012-6-13 20:11','2教室' union all
    select 11,'2012-6-13 20:12','2教室' union all
    select 12,'2012-6-13 20:13','1教室' union all
    select 13,'2012-6-13 20:14','1教室'
    )
    --把数据放到tb1 表里
    select * into tb1 from tb1
    go
    with tb2 as
    (
    --begin_dateteime  end_datetime      i_datetime
    select '2012-6-13 20:04' as begin_dateteime,'2012-6-13 20:07' as end_datetime,'3分钟' as i_datetime union all
    select '2012-6-13 20:10','2012-6-13 20:12','2分钟'
    )
    --把数据放到tb2 表里
    select * into tb2 from tb2
    go
    --把2表的条件实现动态拼写
    declare @sql varchar(max)
    select @sql=ISNULL(@sql,'')+' and (tb1.D_DateTime <= '''+begin_dateteime+''' or tb1.D_DateTime >='''+end_datetime+''')'
    from tb2
    --拼接动态SQL脚本
    set @sql='
    select tb1.*,tb2.i_datetime from tb1 
    left join tb2 on tb1.D_DateTime=tb2.end_datetime
    where '+STUFF(@sql,1,4,'')
    --运行动态脚本
    exec(@sql)
    /*(13 row(s) affected)(2 row(s) affected)
    Serial_No   D_DateTime      address i_datetime
    ----------- --------------- ------- ----------
    1           2012-6-13 20:00 1教室     NULL
    2           2012-6-13 20:01 1教室     NULL
    3           2012-6-13 20:04 2教室     NULL
    6           2012-6-13 20:07 2教室     3分钟
    7           2012-6-13 20:08 1教室     NULL
    8           2012-6-13 20:09 1教室     NULL
    9           2012-6-13 20:10 2教室     NULL
    11          2012-6-13 20:12 2教室     2分钟
    12          2012-6-13 20:13 1教室     NULL
    13          2012-6-13 20:14 1教室     NULL(10 row(s) affected)
    */
      

  2.   

    with tb1 as
    (
    select 1 as Serial_No,'2012-6-13 20:00' as D_DateTime,'1教室' as [address] union all
    select 2,'2012-6-13 20:01','1教室' union all
    select 3,'2012-6-13 20:04','2教室' union all
    select 4,'2012-6-13 20:05','2教室' union all
    select 5,'2012-6-13 20:06','2教室' union all
    select 6,'2012-6-13 20:07','2教室' union all
    select 7,'2012-6-13 20:08','1教室' union all
    select 8,'2012-6-13 20:09','1教室' union all
    select 9,'2012-6-13 20:10','2教室' union all
    select 10,'2012-6-13 20:11','2教室' union all
    select 11,'2012-6-13 20:12','2教室' union all
    select 12,'2012-6-13 20:13','1教室' union all
    select 13,'2012-6-13 20:14','1教室'
    )
    select 
    tb1.*,
    tb2.i_datetime
    from tb1
    left join 
    (
    --begin_dateteime  end_datetime      i_datetime
    select '2012-6-13 20:04' as begin_dateteime,'2012-6-13 20:07' as end_datetime,'3分钟' as i_datetime union all
    select '2012-6-13 20:10','2012-6-13 20:12','2分钟'
    )tb2 on tb1.D_DateTime=tb2.end_datetime
    where  (tb1.D_DateTime <= '2012-6-13 20:04' or tb1.D_DateTime >='2012-6-13 20:07') and (tb1.D_DateTime <= '2012-6-13 20:10' or tb1.D_DateTime >='2012-6-13 20:12')
      

  3.   

    老大,哪个时间不对,我只传递开始时间,和结速时间,然后是根据表2的begin_datetime和end_datetime去查询
    (tb1.D_DateTime <= '2012-6-13 20:04' or tb1.D_DateTime >='2012-6-13 20:07') and (tb1.D_DateTime <= '2012-6-13 20:10' or tb1.D_DateTime >='2012-6-13 20:12')
    这个条件是是根据表2查询出来的,没有指定
      

  4.   

    并且这个字段中,我还会加些其它条件去查询得到tab2
    用Cross join & Cross Apply & Outer Apply 有没有办法,其它就是把tab2的时间段去覆盖tab1的时间,只取开始和结束