I want to find the occuring time of the max wind, the max wind, and total rain from a database. The database have three columns: observerTime, wind and rain, how to generate the SQL statement to get the result ?
问问题
67 次
3 回答
0
select observerTime from t where wind = (select max(wind) from t)
or if you need the last date when it occures
select max(observerTime) from t where wind = (select max(wind) from t)
于 2012-12-05T14:02:50.493 回答
0
您没有提到数据库,但以下其中一项可能会起作用:
select top 1 *, (select sum(rain) from t) as TotalRain
order by wind desc
或者:
select *, (select sum(rain) from t) as TotalRain
from t
order by wind desc
limit 1
或者
select *, (select sum(rain) from t) as TotalRain
from (select *
from t
order by wind desc
) t
where rownum = 1
于 2012-12-05T14:29:17.570 回答
0
你应该能够使用这样的东西:
select t1.observerTime,
t1.wind,
(select sum(rain) from yourtable) TotalRain
from yourtable t1
inner join
(
select max(wind) MaxWind
from yourtable
) t2
on t1.wind = t2.maxwind
由于您使用的是 SQL Server,因此您还可以使用row_number()
:
select observertime,
wind,
(select sum(rain) from yourtable) TotalRain
from
(
select observertime,
wind,
rain,
row_number() over(order by wind desc) rn
from yourtable
) src
where rn = 1
于 2012-12-05T14:46:20.737 回答