求一按输入时间排序的比较复杂的问题,好象条件比较多
有一个表,要求是这样的
编号 抽样时间 抽样值
1 2007-03-04 9:10
2 2007-03-04 11:27
3 2007-03-04 13:34
4 2007-03-04 15:54
1 2007-03-04 17:01
2 2007-03-04 18:34
3 2007-03-04 21:07
4 2007-03-04 23:16
5 2007-03-05 00:34
1 2007-03-05 1:01
2 2007-03-05 3:45
3 2007-03-05 8:59
1 2007-03-05 9:01
就是把一天分成3段:1:01 -- 9:00;9:01 -- 17:00;17:01 -- 第二天1:00
在一个时间段里编号从1开始以时间排序.进入另一个时间段,编号重新从1开始以时间排序.而且一个时间段里抽样的次数不限制.还有个时间段要跨天
这种表怎么做???跪求!!!!
[解决办法]
用存储过程吧。
先写一个函数getStartTime
功能就是获得给定之间对应的时间段的开始时间。
比如给定 2007-03-04 11:27 就可以获得 2007-03-04 09:01
然后再写一个查询排序就可以了。
SELECT ...
FROM ...
ORDER BY getStartTime(抽样时间),编号。
[解决办法]
先不考虑编号,就插入抽样时间和抽样值,编号的问题在查询时实现
--测试数据
create table # (抽样时间 datetime)
insert into # select '2007-03-04 9:10 ' union all
select '2007-03-04 11:27 ' union all
select '2007-03-04 13:34 ' union all
select '2007-03-04 15:54 ' union all
select '2007-03-04 17:01 ' union all
select '2007-03-04 18:34 ' union all
select '2007-03-04 21:07 ' union all
select '2007-03-04 23:16 ' union all
select '2007-03-05 00:34 ' union all
select '2007-03-05 1:01 ' union all
select '2007-03-05 3:45 ' union all
select '2007-03-05 8:59 ' union all
select '2007-03-05 9:01 '
go
--查询
select a=convert(varchar(10),dateadd(hour,-1,抽样时间),120)+(case when convert(varchar(8),dateadd(hour,-1,抽样时间),108) between '00:00:01 ' and '08:00:00 ' then '0 '
when convert(varchar(8),dateadd(hour,-1,抽样时间),108) between '08:00:01 ' and '16:00:00 ' then '1 '
when convert(varchar(8),dateadd(hour,-1,抽样时间),108) between '16:00:01 ' and '23:59:59 ' then '2 ' end),抽样时间
into ##
from #
go
select 编号=(select count(1)+1 from ## where a=t.a and 抽样时间 <t.抽样时间),抽样时间
from ## t
go
--结果
12007-03-04 09:10:00.000
22007-03-04 11:27:00.000
32007-03-04 13:34:00.000
42007-03-04 15:54:00.000
12007-03-04 17:01:00.000
22007-03-04 18:34:00.000
32007-03-04 21:07:00.000
42007-03-04 23:16:00.000
52007-03-05 00:34:00.000
12007-03-05 01:01:00.000
22007-03-05 03:45:00.000
32007-03-05 08:59:00.000
12007-03-05 09:01:00.000
[解决办法]
用jacobsan(梅) 的数据,编号不写了(实际使用加上就好)
create table # (抽样时间 datetime)
insert into # select '2007-03-04 9:10 ' union all
select '2007-03-04 11:27 ' union all
select '2007-03-04 13:34 ' union all
select '2007-03-04 15:54 ' union all
select '2007-03-04 17:01 ' union all
select '2007-03-04 18:34 ' union all
select '2007-03-04 21:07 ' union all
select '2007-03-04 23:16 ' union all
select '2007-03-05 00:34 ' union all
select '2007-03-05 1:01 ' union all
select '2007-03-05 3:45 ' union all
select '2007-03-05 8:59 ' union all
select '2007-03-05 9:01 '
select 抽样时间,
(select count(*) from # where
datepart(hh,dateadd(hh,-1,a.抽样时间))/8=datepart(hh,dateadd(hh,-1,抽样时间))/8
and convert(varchar(8),dateadd(hh,-1,a.抽样时间),112)=
convert(varchar(8),dateadd(hh,-1,抽样时间),112)
and 抽样时间 <a.抽样时间)+1
as 抽样值 from # a order by 抽样时间
drop table #
所影响的行数为 13 行)
抽样时间 抽样值
------------------------------------------------------ -----------
2007-03-04 09:10:00.000 1
2007-03-04 11:27:00.000 2
2007-03-04 13:34:00.000 3
2007-03-04 15:54:00.000 4
2007-03-04 17:01:00.000 1
2007-03-04 18:34:00.000 2
2007-03-04 21:07:00.000 3
2007-03-04 23:16:00.000 4
2007-03-05 00:34:00.000 5
2007-03-05 01:01:00.000 1
2007-03-05 03:45:00.000 2
2007-03-05 08:59:00.000 3
2007-03-05 09:01:00.000 1
(所影响的行数为 13 行)