有一execel表 --时间 0005 为00时05分
日期 时间 风向 风速
110112 0005 320 15
110112 0015 310 18
110112 0108 320 25
110112 0115 310 18
110112 0218 300 15
110112 0225 320 19
110112 1218 310 15
110112 1225 320 12导入SQL数据库后却为
日期 时间 风向 风速 --时间 0005 变为5
110112 5 320 15
110112 15 310 18
110112 108 320 25
110112 115 310 18
110112 218 300 15
110112 225 320 19
110112 1218 310 15
110112 1225 320 12
即原来的时间 时-分变为数值我现在想把上表按小时统计取风速的最大值,该怎么写
想得到的结果为年 月 日 时 风向 风速
11 01 12 00 310 18
11 01 12 01 320 25
11 01 12 02 320 19
11 01 12 12 310 15
日期 时间 风向 风速
110112 0005 320 15
110112 0015 310 18
110112 0108 320 25
110112 0115 310 18
110112 0218 300 15
110112 0225 320 19
110112 1218 310 15
110112 1225 320 12导入SQL数据库后却为
日期 时间 风向 风速 --时间 0005 变为5
110112 5 320 15
110112 15 310 18
110112 108 320 25
110112 115 310 18
110112 218 300 15
110112 225 320 19
110112 1218 310 15
110112 1225 320 12
即原来的时间 时-分变为数值我现在想把上表按小时统计取风速的最大值,该怎么写
想得到的结果为年 月 日 时 风向 风速
11 01 12 00 310 18
11 01 12 01 320 25
11 01 12 02 320 19
11 01 12 12 310 15
insert into @table
select 110112,5,320,15 union all
select 110112,15,310,18 union all
select 110112,108,320,25 union all
select 110112,115,310,18 union all
select 110112,218,300,15 union all
select 110112,225,320,19 union all
select 110112,1218,310,15 union all
select 110112,1225,320,12select
left(日期,2) as 年,
substring(日期,2,2)as 月,
right(日期,2) as 日,
left(right('000'+时间,4),2) as 时,
风向,风速
from @table/*
年 月 日 时 风向 风速
---- ---- ---- ---- ----------- -----------
11 10 12 00 320 15
11 10 12 00 310 18
11 10 12 01 320 25
11 10 12 01 310 18
11 10 12 02 300 15
11 10 12 02 320 19
11 10 12 12 310 15
11 10 12 12 320 12
*/
然后select left(日期,2) 年,right(left(日期,4),2) 月,right(日期,2) 日,left(时间,2) 时,max(风速) from tb group by left(日期,2),right(left(日期,4),2),right(日期,2),left(时间,2)
select
left(日期,2) as 年,
substring(日期,2,2)as 月,
right(日期,2) as 日,
left(right('000'+时间,4),2) as 时,
风向,风速
from @table t
where
not exists(select 1
from @table
where 日期=t.日期
and left(right('000'+时间,4),2)=left(right('000'+t.时间,4),2)
and 风速>t.风速)/**
年 月 日 时 风向 风速
---- ---- ---- ---- ----------- -----------
11 10 12 00 310 18
11 10 12 01 320 25
11 10 12 02 320 19
11 10 12 12 310 15(4 行受影响)
**/
insert into tb select '110112','0005',320,15
insert into tb select '110112','0015',310,18
insert into tb select '110112','0108',320,25
insert into tb select '110112','0115',310,18
insert into tb select '110112','0218',300,15
insert into tb select '110112','0225',320,19
insert into tb select '110112','1218',310,15
insert into tb select '110112','1225',320,12
gowith cte as(
select LEFT(dt,2)as Y,SUBSTRING(dt,3,2)as M,RIGHT(dt,2)as D,left(tm,2)as tm,f,s from tb
)select Y,m,d,tm,(select top 1 f from cte where y=a.y and m=a.m and d=a.d and tm=a.tm and s=a.s)f,s from(
select Y,m,d,tm,MAX(s)s from cte group by Y,m,d,tm
)a
go
drop table tb
/*
Y m d tm f s
---- ---- ---- ---- ----------- -----------
11 01 12 00 310 18
11 01 12 01 320 25
11 01 12 02 320 19
11 01 12 12 310 15(4 行受影响)*/