在论坛中出现的比较难的sql问题:24(生成时间段)

 

最近,在论坛中,遇到了不少比较难的sql问题,虽然自己都能解决,但发现过几天后,就记不起来了,也忘记解决的方法了。

所以,觉得有必要记录下来,这样以后再次碰到这类问题,也能从中获取解答的思路。



1、请问我这样的需求如何实现,看示很简单的问题,实现起来很复杂吗?

现在是这样的问题了

 

USE tempdb 
IF OBJECT_ID('TEST') IS NOT NULL
  DROP TABLE TEST;      --> 如果表TEST不为空,删除表
GO  
CREATE TABLE TEST       --> 创建表,字段及类型如下
(
CalID varchar(20),         -->  编号
CalDate  datetime,         -->  日期字段
CalStime  datetime,         -->  时间字段
CalEtime datetime,         -->   机器号
CalBZ varchar(10)         -->   这是机器要求的时间间隔字段,单位分钟
);
 
GO
 
INSERT TEST --> 向表中插入数据
select 'A01','2011-03-13 00:00:00.000','00:00:00.000','06:59:00.000','丙' union all
select 'A01','2011-03-13 00:00:00.000','07:00:00.000','14:59:00.000','甲' union all
select 'A01','2011-03-13 00:00:00.000','15:00:00.000','21:59:00.000','乙' union all
select 'A01','2011-03-13 00:00:00.000','22:00:00.000','23:59:00.000','丁'

GO 
SELECT * FROM TEST;      --> 表的结构如下

GO


/*
CalID                CalDate                                                CalStime                                               CalEtime                                               CalBZ      
-------------------- ------------------------------------------------------ ------------------------------------------------------ ------------------------------------------------------ ---------- 
A01                  2011-03-13 00:00:00.000                                1900-01-01 00:00:00.000                                1900-01-01 06:59:00.000                                丙
A01                  2011-03-13 00:00:00.000                                1900-01-01 07:00:00.000                                1900-01-01 14:59:00.000                                甲
A01                  2011-03-13 00:00:00.000                                1900-01-01 15:00:00.000                                1900-01-01 21:59:00.000                                乙
A01                  2011-03-13 00:00:00.000                                1900-01-01 22:00:00.000                                1900-01-01 23:59:00.000                                丁
*/

--怎么得到以下结果?
/*
A01   2011-03-13 00:00   2011-03-13 00:29   丙
A01   2011-03-13 00:30   2011-03-13 00:59   丙
A01   2011-03-13 01:00   2011-03-13 01:29   丙
A01   2011-03-13 01:30   2011-03-13 01:59   丙
.....以此类推
A01   2011-03-13 23:00   2011-03-13 23:29   丁
A01   2011-03-13 23:30   2011-03-13 23:59   丁

*/


简单的说,就是如何把表中一个时间范围分段成若干个小时间段?在sql server 2000系统中。

 


我的方法:

 

select CalID,
        
       DATEADD(minute,30*number,cal_stime)  calStime,
       DATEADD(minute,30*(number+1)-1,cal_stime) calEtime,
       --Cal_Stime,
       --Cal_Etime,
       --number,
       CalBZ
from
(
select *,
       convert(varchar(11),CalDate,120)+CONVERT(varchar(5),CalStime,108) Cal_Stime,
       convert(varchar(11),CalDate,120)+CONVERT(varchar(5),CalEtime,108) Cal_Etime
        
from TEST
)t,master..spt_values s
where s.type = 'P'
and s.number >=0
and DATEADD(MINUTE,30*number,Cal_Stime) <=Cal_Etime
/*
CalID    calStime    calEtime    CalBZ
A01    2011-03-13 00:00:00.000    2011-03-13 00:29:00.000    丙
A01    2011-03-13 00:30:00.000    2011-03-13 00:59:00.000    丙
A01    2011-03-13 01:00:00.000    2011-03-13 01:29:00.000    丙
A01    2011-03-13 01:30:00.000    2011-03-13 01:59:00.000    丙
A01    2011-03-13 02:00:00.000    2011-03-13 02:29:00.000    丙
A01    2011-03-13 02:30:00.000    2011-03-13 02:59:00.000    丙
A01    2011-03-13 03:00:00.000    2011-03-13 03:29:00.000    丙
A01    2011-03-13 03:30:00.000    2011-03-13 03:59:00.000    丙
A01    2011-03-13 04:00:00.000    2011-03-13 04:29:00.000    丙
A01    2011-03-13 04:30:00.000    2011-03-13 04:59:00.000    丙
A01    2011-03-13 05:00:00.000    2011-03-13 05:29:00.000    丙
A01    2011-03-13 05:30:00.000    2011-03-13 05:59:00.000    丙
A01    2011-03-13 06:00:00.000    2011-03-13 06:29:00.000    丙
A01    2011-03-13 06:30:00.000    2011-03-13 06:59:00.000    丙
A01    2011-03-13 07:00:00.000    2011-03-13 07:29:00.000    甲
A01    2011-03-13 07:30:00.000    2011-03-13 07:59:00.000    甲
A01    2011-03-13 08:00:00.000    2011-03-13 08:29:00.000    甲
A01    2011-03-13 08:30:00.000    2011-03-13 08:59:00.000    甲
A01    2011-03-13 09:00:00.000    2011-03-13 09:29:00.000    甲
A01    2011-03-13 09:30:00.000    2011-03-13 09:59:00.000    甲
A01    2011-03-13 10:00:00.000    2011-03-13 10:29:00.000    甲
A01    2011-03-13 10:30:00.000    2011-03-13 10:59:00.000    甲
A01    2011-03-13 11:00:00.000    2011-03-13 11:29:00.000    甲
A01    2011-03-13 11:30:00.000    2011-03-13 11:59:00.000    甲
A01    2011-03-13 12:00:00.000    2011-03-13 12:29:00.000    甲
A01    2011-03-13 12:30:00.000    2011-03-13 12:59:00.000    甲
A01    2011-03-13 13:00:00.000    2011-03-13 13:29:00.000    甲
A01    2011-03-13 13:30:00.000    2011-03-13 13:59:00.000    甲
A01    2011-03-13 14:00:00.000    2011-03-13 14:29:00.000    甲
A01    2011-03-13 14:30:00.000    2011-03-13 14:59:00.000    甲
A01    2011-03-13 15:00:00.000    2011-03-13 15:29:00.000    乙
A01    2011-03-13 15:30:00.000    2011-03-13 15:59:00.000    乙
A01    2011-03-13 16:00:00.000    2011-03-13 16:29:00.000    乙
A01    2011-03-13 16:30:00.000    2011-03-13 16:59:00.000    乙
A01    2011-03-13 17:00:00.000    2011-03-13 17:29:00.000    乙
A01    2011-03-13 17:30:00.000    2011-03-13 17:59:00.000    乙
A01    2011-03-13 18:00:00.000    2011-03-13 18:29:00.000    乙
A01    2011-03-13 18:30:00.000    2011-03-13 18:59:00.000    乙
A01    2011-03-13 19:00:00.000    2011-03-13 19:29:00.000    乙
A01    2011-03-13 19:30:00.000    2011-03-13 19:59:00.000    乙
A01    2011-03-13 20:00:00.000    2011-03-13 20:29:00.000    乙
A01    2011-03-13 20:30:00.000    2011-03-13 20:59:00.000    乙
A01    2011-03-13 21:00:00.000    2011-03-13 21:29:00.000    乙
A01    2011-03-13 21:30:00.000    2011-03-13 21:59:00.000    乙
A01    2011-03-13 22:00:00.000    2011-03-13 22:29:00.000    丁
A01    2011-03-13 22:30:00.000    2011-03-13 22:59:00.000    丁
A01    2011-03-13 23:00:00.000    2011-03-13 23:29:00.000    丁
A01    2011-03-13 23:30:00.000    2011-03-13 23:59:00.000    丁
*/

 

 

2、如何取0点开始至今的时间段集合, 间隔1小时 

http://bbs.csdn.net/topics/390706187

如题:如何取0点开始至今的时间段集合, 间隔1小时


 0:00 - 1:00
 1:00 - 2:00
 ...
23:00 - 0:00


下面的代码产生的是纯时间段,不过不好用:

 

select convert(varchar(10),getdate(),120) '当天日期',
       convert(varchar(5),dateadd(hour,s.number,convert(varchar(10),getdate(),120)),108)+'-'+
       convert(varchar(5),dateadd(hour,s.number+1,convert(varchar(10),getdate(),120)),108) '时间段'
from master..spt_values s
where s.type = 'P' and s.number <= 23
/*
当天日期	时间段
2014-02-11	00:00-01:00
2014-02-11	01:00-02:00
2014-02-11	02:00-03:00
2014-02-11	03:00-04:00
2014-02-11	04:00-05:00
2014-02-11	05:00-06:00
2014-02-11	06:00-07:00
2014-02-11	07:00-08:00
2014-02-11	08:00-09:00
2014-02-11	09:00-10:00
2014-02-11	10:00-11:00
2014-02-11	11:00-12:00
2014-02-11	12:00-13:00
2014-02-11	13:00-14:00
2014-02-11	14:00-15:00
2014-02-11	15:00-16:00
2014-02-11	16:00-17:00
2014-02-11	17:00-18:00
2014-02-11	18:00-19:00
2014-02-11	19:00-20:00
2014-02-11	20:00-21:00
2014-02-11	21:00-22:00
2014-02-11	22:00-23:00
2014-02-11	23:00-00:00
*/


这个是产生日期时间段,比较实用:

 

 

if OBJECT_ID('tempdb..#seTable') is not null
   drop table #seTable
   
  create table #seTable(starTime nvarchar(40),endTime nvarchar(40))


insert into #seTable
select --convert(varchar(10),getdate(),120) curr_date,
       convert(nvarchar(19),dateadd(hour,s.number,convert(varchar(10),getdate(),120)),120) start_date,
       convert(nvarchar(19),dateadd(hour,s.number+1,convert(varchar(10),getdate(),120)),120) end_date
from master..spt_values s
where s.type = 'P' and s.number <= 23

select *
from #seTable
/*
starTime	endTime
2014-02-11 00:00:00	2014-02-11 01:00:00
2014-02-11 01:00:00	2014-02-11 02:00:00
2014-02-11 02:00:00	2014-02-11 03:00:00
2014-02-11 03:00:00	2014-02-11 04:00:00
2014-02-11 04:00:00	2014-02-11 05:00:00
2014-02-11 05:00:00	2014-02-11 06:00:00
2014-02-11 06:00:00	2014-02-11 07:00:00
2014-02-11 07:00:00	2014-02-11 08:00:00
2014-02-11 08:00:00	2014-02-11 09:00:00
2014-02-11 09:00:00	2014-02-11 10:00:00
2014-02-11 10:00:00	2014-02-11 11:00:00
2014-02-11 11:00:00	2014-02-11 12:00:00
2014-02-11 12:00:00	2014-02-11 13:00:00
2014-02-11 13:00:00	2014-02-11 14:00:00
2014-02-11 14:00:00	2014-02-11 15:00:00
2014-02-11 15:00:00	2014-02-11 16:00:00
2014-02-11 16:00:00	2014-02-11 17:00:00
2014-02-11 17:00:00	2014-02-11 18:00:00
2014-02-11 18:00:00	2014-02-11 19:00:00
2014-02-11 19:00:00	2014-02-11 20:00:00
2014-02-11 20:00:00	2014-02-11 21:00:00
2014-02-11 21:00:00	2014-02-11 22:00:00
2014-02-11 22:00:00	2014-02-11 23:00:00
2014-02-11 23:00:00	2014-02-12 00:00:00
*/

 

 


 

转载于:https://www.cnblogs.com/momogua/p/8304522.html

你可能感兴趣的:(在论坛中出现的比较难的sql问题:24(生成时间段))