我给大家一组测试数据:编号 日期 最大电压 最大电压日期 最小电压 最小电压日期
1001 2006-06-21 235.3 2006-06-21 22:30 207.7 2006-06-21 18:31
1001 2006-06-22 235.8 2006-06-22 16:11 218.6 2006-06-22 14:17
1001 2006-06-25 238.1 2006-06-25 08:17 226.9 2006-06-25 23:49我需要得到的结果:编号 日期 最大电压 最大电压日期 最小电压 最小电压日期
1001 无 238.1 2006-06-25 08:17 207.7 2006-06-21 18:31请各位高手帮帮忙啊!分数不够再加!
1001 2006-06-21 235.3 2006-06-21 22:30 207.7 2006-06-21 18:31
1001 2006-06-22 235.8 2006-06-22 16:11 218.6 2006-06-22 14:17
1001 2006-06-25 238.1 2006-06-25 08:17 226.9 2006-06-25 23:49我需要得到的结果:编号 日期 最大电压 最大电压日期 最小电压 最小电压日期
1001 无 238.1 2006-06-25 08:17 207.7 2006-06-21 18:31请各位高手帮帮忙啊!分数不够再加!
select top 1 a,b='无',c=(select max(c) from test),d = (select d from test where c = (select max(c) from test)),e=(select min(e) from test),f = (select f from test where e = (select min(e) from test)) from test
a,b,c,d,e,f分别对应你上面的字段
insert t select 1001, '2006-06-21', 235.3, '2006-06-21 22:30', 207.7, '2006-06-21 18:31'
union all select 1001, '2006-06-22', 235.8, '2006-06-22 16:11', 218.6, '2006-06-22 14:17'
union all select 1001, '2006-06-25', 238.1, '2006-06-25 08:17', 226.9, '2006-06-25 23:49'select distinct A.编号,N'无' [日期],A.最大电压,
(select top 1 convert(varchar(16),最大电压日期,120) from t where 最大电压=A.最大电压) [最大电压日期],
B.最小电压 ,
(select top 1 convert(varchar(16),最小电压日期,120) from t where 最小电压=B.最小电压) [最小电压日期]
from t
join (select 编号,max(最大电压) [最大电压] from t group by 编号) A on A.编号=t.编号
join (select 编号,min(最小电压) [最小电压] from t group by 编号) B on B.编号=t.编号drop table t
/*
编号 日期 最大电压 最大电压日期 最小电压 最小电压日期
----------- ---- ------------ ---------------- ------------ ----------------
1001 无 238.1 2006-06-25 08:17 207.7 2006-06-21 18:31
*/
(Select MaxPressure,MaxDate From test
where CurrentlyDate >= '2006-01-01'
and CurrentlyDate <= '2006-12-31') as myMaxTest Order by MaxPressure Desc) as MaxTest,
(Select Top 1 * From
(Select MinPressure,MinDate From test
where CurrentlyDate >= '2006-01-01'
and CurrentlyDate <= '2006-12-31') as myMinTest Order by MinPressure) as MinTest
MaxDate 最大电压日期
MinPressure 最小电压
MinDate 最小电压日期用SQL要过滤二次数据,所以性能不太好,存储过程只用一次
Select MaxPressure,MaxDate,MinPressure,MinDate From test
where CurrentlyDate >= '2006-01-01'
and CurrentlyDate <= '2006-12-31')
至于编号和日期,你可以在输出的时候再加上
insert t select 1001, '2006-06-21', 235.3, '2006-06-21 22:30', 207.7, '2006-06-21 18:31'
union all select 1001, '2006-06-22', 235.8, '2006-06-22 16:11', 218.6, '2006-06-22 14:17'
union all select 1001, '2006-06-25', 238.1, '2006-06-25 08:17', 226.9, '2006-06-25 23:49'select distinct A.编号,N'无' [日期],A.最大电压,
(select top 1 convert(varchar(16),最大电压日期,120) from t where 最大电压=A.最大电压) [最大电压日期],
B.最小电压 ,
(select top 1 convert(varchar(16),最小电压日期,120) from t where 最小电压=B.最小电压) [最小电压日期]
from t
join (select 编号,max(最大电压) [最大电压] from t group by 编号) A on A.编号=t.编号
join (select 编号,min(最小电压) [最小电压] from t group by 编号) B on B.编号=t.编号drop table t
/*
编号 日期 最大电压 最大电压日期 最小电压 最小电压日期
----------- ---- ------------ ---------------- ------------ ----------------
1001 无 238.1 2006-06-25 08:17 207.7 2006-06-21 18:31
*/佩服敬业精神 没的讲