有如下表结构
日期 降水 温度
具体数据
1970-1-1 20mm 20度
1970-1-2 21mm 10度
1970-1-3 25mm 8度
1970-1-4 21mm 25度
1970-1-5 20mm 12度
1970-1-6 23mm 13度
1970-1-7 20mm 12度
1970-1-8 20mm 12度
1970-1-9 10mm 15度
1970-1-10 24mm 17度
依次类推,表中的每日数据直到2001年12月31日
各位大侠:我需要查询1970年到2001年每两天(当天和次日)的温度变化幅度(当日-次日)小于等于正负10度,并且该两日最大降水大于20mm的当天记录,请问各位大侠如何写sql语句我要查询的数据结果如下:
1970-1-2 21mm 10度
1970-1-5 20mm 12度
1970-1-6 23mm 13度
1970-1-9 10mm 15度
日期 降水 温度
具体数据
1970-1-1 20mm 20度
1970-1-2 21mm 10度
1970-1-3 25mm 8度
1970-1-4 21mm 25度
1970-1-5 20mm 12度
1970-1-6 23mm 13度
1970-1-7 20mm 12度
1970-1-8 20mm 12度
1970-1-9 10mm 15度
1970-1-10 24mm 17度
依次类推,表中的每日数据直到2001年12月31日
各位大侠:我需要查询1970年到2001年每两天(当天和次日)的温度变化幅度(当日-次日)小于等于正负10度,并且该两日最大降水大于20mm的当天记录,请问各位大侠如何写sql语句我要查询的数据结果如下:
1970-1-2 21mm 10度
1970-1-5 20mm 12度
1970-1-6 23mm 13度
1970-1-9 10mm 15度
如果只判断当天跟次日的,那 1970-1-1 20mm 20度 为什么结果中没有呢?
1970-1-2 21mm 10度 第二天10度,相差10度,满足, 最大降雨21mm > 20mm ,满足
--环境
create table tab
(
日期 datetime,
降水 varchar(10),
温度 varchar(10)
)
insert into tab select '1970-1-1', '20mm', '20度'
insert into tab select '1970-1-2', '21mm', '10度'
insert into tab select '1970-1-3', '25mm', '8度'
insert into tab select '1970-1-4', '21mm', '25度'
insert into tab select '1970-1-5', '20mm', '12度'
insert into tab select '1970-1-6', '23mm', '13度'
insert into tab select '1970-1-7', '20mm', '12度'
insert into tab select '1970-1-8', '20mm', '12度'
insert into tab select '1970-1-9', '10mm', '15度'
insert into tab select '1970-1-10', '24mm', '17度'--查询
select a.*
from tab a left join tab b on a.日期 = dateadd(day,-1,b.日期)
where abs(cast(replace(a.温度,'度','') as int) - cast(replace(b.温度,'度','') as int)) <= 10
and (cast(replace(a.降水,'mm','') as int) > 20 or cast(replace(b.降水,'mm','') as int) > 20)--结果
/*
日期 降水 温度
----------------------- ---------- ----------
1970-01-01 00:00:00.000 20mm 20度
1970-01-02 00:00:00.000 21mm 10度
1970-01-05 00:00:00.000 20mm 12度
1970-01-06 00:00:00.000 23mm 13度
1970-01-09 00:00:00.000 10mm 15度(5 行受影响)
*/
--删除
drop table tab
--另外建议降水和温度里不要带上单位,占了空间,还没有什么太大作用
declare @T table(RowNo int identity(1,1) , [Date] datetime, Precipitation int, Temperature int )
insert into @T
select '1970-1-1', '20', '20' union all
select '1970-1-2', '21', '10' union all
select '1970-1-3', '25', '8' union all
select '1970-1-4', '21', '25' union all
select '1970-1-5', '20', '12' union all
select '1970-1-6', '23', '13' union all
select '1970-1-7', '20', '12' union all
select '1970-1-8', '20', '12' union all
select '1970-1-9', '10', '15' union all
select '1970-1-10', '24', '17' select CONVERT(nvarchar(8),T1.[Date],105) as N'日期',
convert(nvarchar(10),T1.Precipitation) + 'mm' as N'降水',
convert(nvarchar(10),T1.Temperature) + N'度' as N'温度'
from @T as T1
join @T as T2 on T1.RowNo + 1 = T2.RowNo and abs(T2.Temperature - T1.Temperature)<=10
where (T1.Precipitation > 20 or T2.Precipitation >20)运行结果
+===============================================
日期 降水 温度
01-01-19 20mm 20度
02-01-19 21mm 10度
05-01-19 20mm 12度
06-01-19 23mm 13度
09-01-19 10mm 15度
CONVERT(nvarchar(10),T1.[Date],20) as N'日期' ^^
select *
from TestTable t1
where (t1.Temp - (select t2.Temp from TestTable t2 where t2.id = t1.id + 1)) <= 10 and (t1.Rain > 20 or (select t3.Rain from TestTable t3 where t3.id = t1.id + 1) > 20)我这里假设你的表中有一个id,不知道以上代码对你有没有帮助
create table test(
id int IDENTITY(1,1) NOT NULL PRIMARY key,
addtime datetime,
water int,
Temperature int
)
insert into test(addtime,water,temperature)(
select '1970-1-1', 20, 20 union
select '1970-1-2', 21, 10 union
select '1970-1-3', 25, 8 union
select '1970-1-4', 21, 25 union
select '1970-1-5', 20, 12 union
select '1970-1-6', 23, 13 union
select '1970-1-7', 20, 12 union
select '1970-1-8', 20, 12 union
select '1970-1-9', 10, 15
)
select A.* from test A,test B where A.addtime=B.addtime+1
and (A.water>20 or B.water>20) and abs(A.temperature-B.temperature)<=10
order by A.addtime
(9 行受影响)
id addtime water Temperature
----------- ----------------------- ----------- -----------
2 1970-01-02 00:00:00.000 21 10
3 1970-01-03 00:00:00.000 25 8
6 1970-01-06 00:00:00.000 23 13
7 1970-01-07 00:00:00.000 20 12drop table test