一、基础数据说明
数据库: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谢谢
数据库: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谢谢
解决方案 »
- 想要两台SQL2005服务器间某个表的数据每日自动同步,哪种方法比较好?
- 如何把Image字段的数据转化成Text类型字段的数据
- 问一个关于子查询的问题!
- 差异备份如何恢复
- 在sql server2000中如何查询datetime型的数据?
- SQL SERVER问题请教会的老手们!程序出错 麻烦纠错指点!
- 误删除表后如何用LOG EXPLOER恢复
- 关于时间格式输入的问题!
- 什么地方可以找到 interbase 的 ODBC驱动
- 求一个SQL,最好不用反复查询
- 高分求解!!! 怎么样在企业管理器里,让新增加的用户只能看到有权限操作的表和存储过程
- 内存不足,死锁 , 错误: 701,严重性: 17,状态: 123。快急疯了!!
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
该表里有很多记录,不仅仅只有这两条。
无法按
select 'a',convert(date,'2013-06-01'),'2013-06-06',1 union all
select 'b','2013-06-05','2013-06-08',2
这个来指定相应的日期
该表里有很多记录,不仅仅只有这两条。
无法按
select 'a',convert(date,'2013-06-01'),'2013-06-06',1 union all
select 'b','2013-06-05','2013-06-08',2
这个来指定相应的日期那你总得有个范围吧,不行就先写个语句确定范围,再用下面的
惭愧,没有理解透
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表从何来的?谢谢
结果只出来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