[英]How to get max value and corresponding date in SQL Server
I have a table of locations, dates, and altitude values.我有一张位置、日期和海拔值的表格。 The query below is easy, but I'm trying to get min/max dates that correspond to the min/max values of altitude.
下面的查询很简单,但我试图获取与高度的最小/最大值相对应的最小/最大日期。
SELECT location,
max(datetime) as max_date
COUNT(Altitude) as COUNT,
AVG(Altitude) as AVG,
MIN(Altitude) as MIN,
max(Altitude) as MAX
FROM mydatabase
GROUP BY location
For every location select max and min altitude and max min datetime for them.对于每个位置 select 最大和最小高度以及它们的最大最小日期时间。
select location, Altitude, min([datetime]) dtmin, max([datetime]) dtmax
from (
SELECT location, Altitude, [datetime]
, min(Altitude) over(partition by location) as aMIN
, max(Altitude) over(partition by location) as aMAX
FROM mydatabase
) t
where Altitude in (aMIN, aMAX)
group by location, Altitude
order by location, Altitude;
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.