我首先通过查询得到一张表,类似如下
channels      date  
 cctv1       2007-10-12
 cctv1       2007-10-13
 cctv2       2007-10-10
 cctv3       2007010-12
现在要求输入一个时间段,比如@startDate = '20071010' @endDate = '20071014',则我需要得到没有频道在这个时间段中没有时间纪录的信息,如下
channels      missingdate
cctv1          2007-10-10
cctv1          2007-10-11
cctv1          2007-10-14
cctv2          2007-10-11
cctv2          2007-10-12
cctv2          2007-10-13
cctv2          2007-10-14
cctv3          2007-10-10
cctv3          2007-10-11
cctv3          2007-10-13
cctv3          2007-10-14这个Select查询语句如何写呀??
拜托各位了,很着急!

解决方案 »

  1.   

    declare @T table(channels nvarchar(5),     [date]  datetime)
    insert @T select 'cctv1',       '2007-10-12' 
    insert @T select 'cctv1',       '2007-10-13' 
    insert @T select 'cctv2',       '2007-10-10' 
    insert @T select 'cctv3',       '2007-10-12' 
    declare @startDate datetime,@endDate datetime
    select @startDate = '20071010', @endDate ='20071014'declare @ta table(tdate nvarchar(10))
    while @startDate!>@endDatebegin
    insert @ta select convert(varchar(10),@startDate,120)
    set @startDate=dateadd(d,1,@startDate)
    endselect 
    distinct t.channels,t2.[tdate]  
    from 
    @T t cross join @ta t2 
    where 
    not exists(select 1 from @t where channels=t.channels and [date]=t2.tdate)
    order by channels
    channels tdate      
    -------- ---------- 
    cctv1    2007-10-10
    cctv1    2007-10-11
    cctv1    2007-10-14
    cctv2    2007-10-11
    cctv2    2007-10-12
    cctv2    2007-10-13
    cctv2    2007-10-14
    cctv3    2007-10-10
    cctv3    2007-10-11
    cctv3    2007-10-13
    cctv3    2007-10-14(所影响的行数为 11 行)
      

  2.   


    select 
     t.channels,t2.[tdate]  
    from 
    @T t cross join @ta t2 
    where 
    not exists(select 1 from @t where channels=t.channels and [date]=t2.tdate)
    group by t.channels,t2.[tdate] 
    order by channels
      

  3.   

    或用not in 
    declare @T table(channels nvarchar(5),     [date]  datetime)
    insert @T select 'cctv1',       '2007-10-12' 
    insert @T select 'cctv1',       '2007-10-13' 
    insert @T select 'cctv2',       '2007-10-10' 
    insert @T select 'cctv3',       '2007-10-12' 
    declare @startDate datetime,@endDate datetime
    select @startDate = '20071010', @endDate ='20071014'declare @ta table(tdate nvarchar(10))
    while @startDate!>@endDatebegin
    insert @ta select convert(varchar(10),@startDate,120)
    set @startDate=dateadd(d,1,@startDate)
    endselect 
     t.channels,t2.[tdate]  
    from 
    @T t cross join @ta t2 
    where 
    tdate not in (select [date] from @t where channels=t.channels )--------not in 
    group by 
    t.channels,t2.[tdate] 
    order by channels
      

  4.   

    create table tb(channels varchar(10),date datetime)
    insert into tb values('cctv1','2007-10-12') 
    insert into tb values('cctv1','2007-10-13') 
    insert into tb values('cctv2','2007-10-10') 
    insert into tb values('cctv3','2007-10-12')
    go
    declare @dt1 as datetime
    declare @dt2 as datetime
    set @dt1 = '2007-10-10'
    set @dt2 = '2007-10-14'select top 100 id=identity( int,0,1) into tmp from syscolumns a,syscolumns bselect distinct a.channels,convert(varchar(10),dateadd(day,b.id,@dt1),120) date from tmp b cross join tb a 
    where dateadd(day,b.id,@dt1) <= @dt2 and a.channels+convert(varchar(10),dateadd(day,b.id,@dt1),120) not in
    (select channels + convert(varchar(10),date,120) from tb)drop table tb,tmp/*
    channels   date       
    ---------- ---------- 
    cctv1      2007-10-10
    cctv1      2007-10-11
    cctv1      2007-10-14
    cctv2      2007-10-11
    cctv2      2007-10-12
    cctv2      2007-10-13
    cctv2      2007-10-14
    cctv3      2007-10-10
    cctv3      2007-10-11
    cctv3      2007-10-13
    cctv3      2007-10-14(所影响的行数为 11 行)
    */
      

  5.   

    这里有你的解决方案
    http://www.zsn123.cn
      

  6.   

    declare @startDate datetime,@endDate datetime
    select @startDate = '2007-10-10', @endDate ='2007-10-14'
    select d.*,(a.i  + b.i* 10 + c.i* 100) + @startDate as missingdate
    from 

    select 0 as i 
    union all select 1 union all select 2 union all select 3 
    union all select 4 union all select 5 union all select 6 
    union all select 7 union all select 8 union all select 9 
    ) a 


    select 0 as i 
    union all select 1 union all select 2 union all select 3
    union all select 4 union all select 5 union all select 6
    union all select 7 union all select 8 union all select 9
    ) b 


    select 0 as i 
    union all select 1 union all select 2 union all select 3
    union all select 4 union all select 5 union all select 6
    union all select 7 union all select 8 union all select 9
    ) c 
    ,
    (
    select distinct channels 
    from T
    ) d
    where (a.i  + b.i* 10 + c.i* 100) + @startDate <= @endDate
    and 
    not exists 
    (
    select 1 
    from T 
    where channels = d.channels 
    and
    (a.i  + b.i* 10 + c.i* 100) + @startDate = [date]
    )
    order by d.channels,missingdate