日期:2014-05-18  浏览次数:20599 次

根据类型、时间求一列数据的差
Ark_ID int 4
Up_Time datetime
Temperature_Ark numeric 9

Ark_ID Up_Time Temperature_Ark  

1 2011-11-23 7:50:02 258452.25
2 2011-11-23 7:50:02 201406.7
3 2011-11-23 7:50:02 257207.78
4 2011-11-23 7:50:02 262221.55
5 2011-11-23 7:50:02 377021.81
6 2011-11-23 7:50:02 150993.8
7 2011-11-23 7:50:02 4060757.56
8 2011-11-23 7:50:02 45649.9
1 2011-11-23 8:00:02 258452.77
2 2011-11-23 8:00:02 201408.04
3 2011-11-23 8:00:02 257209.57
4 2011-11-23 8:00:02 262222.54
5 2011-11-23 8:00:02 377022.72
6 2011-11-23 8:00:02 150994.81
7 2011-11-23 8:00:02 4060758.16
8 2011-11-23 8:00:02 45650.02
1 2011-11-23 8:10:02 258453.55
2 2011-11-23 8:10:02 201409.22
3 2011-11-23 8:10:02 257211.97
4 2011-11-23 8:10:02 262223.86
5 2011-11-23 8:10:02 377023.89
6 2011-11-23 8:10:02 150996.19
7 2011-11-23 8:10:02 4060758.73
8 2011-11-23 8:10:02 45650.1
1 2011-11-23 8:20:02 258454.12
2 2011-11-23 8:20:02 201410.65
3 2011-11-23 8:20:02 257214.86
4 2011-11-23 8:20:02 262225.7
5 2011-11-23 8:20:02 377025.42
6 2011-11-23 8:20:02 150997.86
7 2011-11-23 8:20:02 4060759.13
8 2011-11-23 8:20:02 45650.22
1 2011-11-23 8:30:02 258454.85
2 2011-11-23 8:30:02 201412.42
3 2011-11-23 8:30:02 257217.63
4 2011-11-23 8:30:02 262227.86
5 2011-11-23 8:30:02 377027.19
6 2011-11-23 8:30:02 150999.21
7 2011-11-23 8:30:02 4060759.64
8 2011-11-23 8:30:02 45650.35
1 2011-11-23 8:40:02 258455.94
2 2011-11-23 8:40:02 201414.1
3 2011-11-23 8:40:02 257220.36
4 2011-11-23 8:40:02 262230.37
5 2011-11-23 8:40:02 377029.9
6 2011-11-23 8:40:02 151000.57
7 2011-11-23 8:40:02 4060760.18
8 2011-11-23 8:40:02 45650.61
1 2011-11-23 8:50:02 258457.18
2 2011-11-23 8:50:02 201415.6
3 2011-11-23 8:50:02 257223.05
4 2011-11-23 8:50:02 262232.65
5 2011-11-23 8:50:02 377032.48
6 2011-11-23 8:50:02 151002.01
7 2011-11-23 8:50:02 4060760.75
8 2011-11-23 8:50:02 45650.72
1 2011-11-23 9:00:02 258458.99
2 2011-11-23 9:00:02 201417.48
3 2011-11-23 9:00:02 257225.87
4 2011-11-23 9:00:02 262234.79
5 2011-11-23 9:00:02 377035.13
6 2011-11-23 9:00:02 151003.65
7 2011-11-23 9:00:02 4060761.32
8 2011-11-23 9:00:02 45650.86
1 2011-11-23 9:10:02 258460.32
2 2011-11-23 9:10:02 201419.08
3 2011-11-23 9:10:02 257228.73
4 2011-11-23 9:10:02 262236.89
5 2011-11-23 9:10:02 377037.73
6 2011-11-23 9:10:02 151005.07
7 2011-11-23 9:10:02 4060761.88
8 2011-11-23 9:10:02 45651.02
根据Ark_ID、Up_Time这二个变量计算Temperature_Ark差值,根据8个类型Ark_ID每一天计算一次Temperature_Ark,只要计算这一天的最后一条数据减去一天最开始的数据,求那个好人帮忙

------解决方案--------------------
SQL code
select Ark_ID, convert(varchar(10),Up_Time,120) , sum(Temperature_Ark) from
(
select t.* from tb t where Up_Time = (select max(Up_Time) from tb where Ark_ID = t.Ark_ID and datediff(dd,Up_Time,t.Up_Time) = 0)
union all
select t.Ark_ID ,t.Up_Time ,-t.Temperature_Ark  from tb t where Up_Time = (select min(Up_Time) from tb where Ark_ID = t.Ark_ID and datediff(dd,Up_Time,t.Up_Time) = 0)
) k
group by Ark_ID, convert(varchar(10),Up_Time,120)

------解决方案--------------------
SQL code
create table tb(Ark_ID int,Up_Time datetime,Temperature_Ark decimal(9,2))
insert into tb select 1,'2011-11-23 7:50:02',258452.25
insert into tb select 2,'2011-11-23 7:50:02',201406.7
insert into tb select 3,'2011-11-23 7:50:02',257207.78
insert into tb select 4,'2011-11-23 7:50:02',262221.55
insert into tb select