帮忙看看这个帖子啊
http://topic.csdn.net/u/20100720/06/9f2b57b2-feae-4ff6-a2cd-87e1ae4bae56.html?seed=2007087652&r=67109019#r_67109019我有数据集
id(int)addtime(datetime)log(varchar)  
  1 2010-07-01 aa
  2 2010-07-03 bb
  3 2010-07-07 vv  比如我想从结果集中查询
2010-07-01 到 2010-07-10 这个时间段的数据
从数据表中我们能看到只有3天是有数据的
但是我希望生成的是一个完整的符合这个时间段的数据
我想要的查询结果是   
id(int)addtime(datetime)log(varchar)  
  1 2010-07-01 aa
null 2010-07-02 null
  2 2010-07-03 bb
null 2010-07-04 null
null 2010-07-05 null
null 2010-07-06 null
  3 2010-07-07 vv  
null 2010-07-08 null
null 2010-07-09 null
null 2010-07-10 null
就算是那天没有记录的,我也要显示出来
这样子的查询要怎么去写?
他们给出这样子的解答
declare @ldt_begin datetime,@ldt_end datetime
declare @lt_dt table(ldt_date datetime)
declare @ldt_tmp datetime
set @ldt_begin='2010-07-01'
set @ldt_end='2010-07-10'set @ldt_tmp=@ldt_beginwhile @ldt_tmp<=@ldt_end
begin
 insert into @lt_dt(ldt_date) select @ldt_tmp
 set @ldt_tmp=dateadd(day,1,@ldt_tmp)
endselect * from @lt_dt2010-07-01 00:00:00.000
2010-07-02 00:00:00.000
2010-07-03 00:00:00.000
2010-07-04 00:00:00.000
2010-07-05 00:00:00.000
2010-07-06 00:00:00.000
2010-07-07 00:00:00.000
2010-07-08 00:00:00.000
2010-07-09 00:00:00.000
2010-07-10 00:00:00.000select a.id,isnull(a.addtime,b.ldt_date),a.log  
from 表 a
right join @lt_dt b
on a.addtime=b.ldt_date
where a.addtime>=@ldt_begin and a.addtime<=@ldt_end测试看一下。我测试了一下,后面的条件 a.addtime=b.ldt_date 本来数据库中就没有的日期 用这个条件怎么判断得出来 那就等于是没有数据了
后面的where也是。很是纳闷

解决方案 »

  1.   

    --建立测试环境
    IF OBJECT_ID('tb') IS NOT NULL  DROP TABLE tb
    GO
    CREATE TABLE tb
    (
    id int identity, 
    addtime varchar(10),
    log varchar(20), 
        CONSTRAINT PK_TB PRIMARY KEY (id)
    )
    GO
    INSERT TB
    SELECT '2010-7-1','aa' union all
    SELECT '2010-7-3','bb' union all
    SELECT '2010-7-7','vv' 
    --查询
    declare @ldt_begin datetime,@ldt_end datetime
    set @ldt_begin='2010-07-01'
    set @ldt_end='2010-07-10'select b.id,convert(varchar(10),dateadd(day,number,@ldt_begin),120) dt,b.[log]
    from master..spt_values a left join TB b on datediff(dd,dateadd(day,number,@ldt_begin),b.addtime)=0
    where type='P' and number between 0 and datediff(day,@ldt_begin,@ldt_end)--结果
    /*
    id          dt         log
    ----------- ---------- --------------------
    1           2010-07-01 aa
    NULL        2010-07-02 NULL
    2           2010-07-03 bb
    NULL        2010-07-04 NULL
    NULL        2010-07-05 NULL
    NULL        2010-07-06 NULL
    3           2010-07-07 vv
    NULL        2010-07-08 NULL
    NULL        2010-07-09 NULL
    NULL        2010-07-10 NULL(10 行受影响)
    */
     
      

  2.   

    [sql server] 得到连续日期查询  
    http://blog.csdn.net/xys_777/archive/2010/06/20/5681208.aspx如果用master..values那么
    在sql2000 最大到255
    sql 2005 最大到 1024
      

  3.   

    declare @table1 table (id int,addtime datetime)
    insert into @table1 select 1,'2010-03-01'
    insert into @table1 select 2,'2010-03-03'
    insert into @table1 select 3,'2010-03-04'
    insert into @table1 select 4,'2010-03-06'
    insert into @table1 select 5,'2010-03-07'
    insert into @table1 select 6,'2010-03-12'
    insert into @table1 select 7,'2010-03-15'
    insert into @table1 select 8,'2010-03-23'
    insert into @table1 select 9,'2010-03-25'
    insert into @table1 select 10,'2010-04-01'
    insert into @table1 select 11,'2010-04-03'
    insert into @table1 select 12,'2010-04-04'
    insert into @table1 select 13,'2010-04-07'
    insert into @table1 select 14,'2010-04-12'
    insert into @table1 select 15,'2010-04-15'
    insert into @table1 select 16,'2010-04-23'
    insert into @table1 select 17,'2010-04-25'declare @stime datetime,@etime datetime
    declare @table2 table(ldt_date datetime)
    declare @mtime datetime
    set @stime='2010-03-01'
    set @etime='2010-03-30'set @mtime=@stimewhile @mtime<=@etime
    begin
     insert into @table2(ldt_date) select @mtime
     set @mtime=dateadd(day,1,@mtime)
    end--select * from @table2select a.ID , isnull(a.addtime,b.ldt_date)
    from @table1 a
    left JOIN 
    @table2 B 
    ON a.addtime=b.ldt_date
    WHERE  a.addtime >= @stime and a.addtime <= @etime
    结果:
    1 2010-03-01 00:00:00.000
    2 2010-03-03 00:00:00.000
    3 2010-03-04 00:00:00.000
    4 2010-03-06 00:00:00.000
    5 2010-03-07 00:00:00.000
    6 2010-03-12 00:00:00.000
    7 2010-03-15 00:00:00.000
    8 2010-03-23 00:00:00.000
    9 2010-03-25 00:00:00.000不是我想要的呀  
      

  4.   


    master..spt_values 这个是一个什么东西呢?