一、基础数据说明
数据库:MSSQL
数据表:表A
字段组成:
[align=left]name 
begin_date(类型为:datatime)
end_date(类型为:datatime)
type[/align]二、表的内容
Name      begin_date                      end_date                 type
------------------------------------------------------------------------------
A         2013-06-01 00:00:00             2013-06-06 00:00:00        1
B         2013-06-05 00:00:00             2013-06-08 00:00:00        2
三、想实现的效果
请教:怎样通过SQL把begin_date和end_date之间的日期拆解后分别显示出来呢,效果如下
Name      time                        type
--------------------------------------------------------------
A         2013-06-01 00:00:00          1    
A         2013-06-02 00:00:00          1
A         2013-06-03 00:00:00          1 
A         2013-06-04 00:00:00          1  
A         2013-06-05 00:00:00          1
A         2013-06-06 00:00:00          1
B         2013-06-05 00:00:00          2
B         2013-06-06 00:00:00          2
B         2013-06-07 00:00:00          2  
B         2013-06-08 00:00:00          2 
B         2013-06-09 00:00:00          2谢谢

解决方案 »

  1.   


    with tb(a,begin_date,end_date,type) as(
    select 'a',convert(date,'2013-06-01'),'2013-06-06',1 union all
    select 'b','2013-06-05','2013-06-08',2
    )
    ,tc as(
    select a, begin_date,type from tb
    union all
    select tb.a,dateadd(day,1,tc.begin_date),tb.type 
    from tc,tb where tc.begin_date<=tb.end_date and tc.a=tb.a
    )
    select * from tc order by 1,2
      

  2.   


    该表里有很多记录,不仅仅只有这两条。
    无法按
    select 'a',convert(date,'2013-06-01'),'2013-06-06',1 union all
    select 'b','2013-06-05','2013-06-08',2
    这个来指定相应的日期
      

  3.   


    该表里有很多记录,不仅仅只有这两条。
    无法按
    select 'a',convert(date,'2013-06-01'),'2013-06-06',1 union all
    select 'b','2013-06-05','2013-06-08',2
    这个来指定相应的日期那你总得有个范围吧,不行就先写个语句确定范围,再用下面的
      

  4.   


    惭愧,没有理解透
    tb表这部分可以理解,相当于把数据按条件先筛选出来,作为tb表在tc表这部分,没理解。
    tc as(
    select a, begin_date,type from tb
    union all
    select tb.a,dateadd(day,1,tc.begin_date),tb.type 
    from tc,tb where tc.begin_date<=tb.end_date and tc.a=tb.a
    )这里边的from tc,这个tc表从何来的?谢谢
      

  5.   


    结果只出来4个。
    a 2013-06-01 00:00:00.000 1
    b 2013-06-05 00:00:00.000 2
    a 2013-06-02 00:00:00.000 1
    b 2013-06-06 00:00:00.000 2