我的数据库中有两个表- gl_weather_locations和gl_weather_data。
我需要从locations表中选择位置名称、纬度和经度,然后只获取数据表中相应经度/纬度的最新记录集-可以有多个记录集,因为数据库是由每2小时更新一次的web服务填充的。我们需要保存档案数据,所以我们不能只保留最近的一组记录,我们需要保留所有的记录。但是我有点困惑于如何将我的查询限制在最近的集合(每个位置有3个数据记录的倍数-一个关于风速,一个关于风向,一个关于海浪高度-我们需要获得所有三个中的最新的)。
到目前为止,我所拥有的是:
SELECT l.location_name, l.location_lat, l.location_long, d.weather_type,
d.weather_value, d.weather_unit
FROM gl_weather_locations l
LEFT JOIN gl_weather_data d ON l.location_lat = d.weather_lat
(在我们的例子中,我们使用的位置是有限的,并且绝对确认我们没有多个位置具有完全相同的纬度,因此我们可以安全地只在纬度上进行连接,而不需要同时比较经度。)
我对如何确保只获得每个纬度的最新记录集感到困惑。如果有帮助,一个集合中的所有记录的weather_time字段都是相同的(即我们有10个位置,每个web服务的每个位置有3个记录,所以我们有30个记录在weather_time中具有相同的值)。我能以某种方式在order by或group by之类的地方使用它吗?
发布于 2011-02-24 20:56:19
第二个FROM (通过SQL-Select)应该会让你得到每一个经度/经度,每个类别各自的最大时间。然后,整个查询将位置链接到那个(MaxTimes)别名,然后根据与要测试的各个分类单元匹配的最大时间重新连接到天气数据。如果得到任何null值,只需将这些列包装为NVL( whatever_column,'‘)作为ResultColumn即可
select WLoc.ID,
WLoc.Location_Name,
WLoc.Location_Lat,
WLoc.Location_Long,
IFNULL( MaxTimes.WindDirTime, '' ) WindDirTime,
IFNULL( MaxTimes.WindSpdTime, '' ) WindSpdTime,
IFNULL( MaxTimes.WaveHeightTime, '' ) WaveHeightTime,
IFNULL( ByWindDir.Weather_Unit, '' ) WindDirection,
IFNULL( ByWindDir.Weather_Value, '' ) WindDirValue,
IFNULL( ByWindSpd.Weather_Unit, '' ) SpeedUnit,
IFNULL( ByWindSpd.Weather_Value, '' ) WindSpeed,
IFNULL( ByWaveHeight.Weather_Unit, '' ) WaveUnit,
IFNULL( ByWaveHeight.Weather_Value, '' ) WaveHeight
from
gl_weather_locations WLoc
left join (select weather_lat,
weather_long,
max( case when weather_type = "Wind Direction" then weather_time end ) WindDirTime,
max( case when weather_type = "Wind Speed" then weather_time end ) WindSpdTime,
max( case when weather_type = "Wave Height" then weather_time end ) WaveHeightTime
from
gl_weather_data
group by
1, 2 ) MaxTimes
on WLoc.Location_Lat = MaxTimes.Weather_Lat
and WLoc.Location_Long = MaxTimes.Weather_Long
left join gl_weather_data ByWindDir
on MaxTimes.weather_lat = ByWindDir.weather_lat
and MaxTimes.weather_long = ByWindDir.weather_long
and MaxTimes.WindDirTime = ByWindDir.weather_time
and ByWindDir.weather_type = "Wind Direction"
left join gl_weather_data ByWindSpd
on MaxTimes.weather_lat = ByWindSpd.weather_lat
and MaxTimes.weather_long = ByWindSpd.weather_long
and MaxTimes.WindSpdTime = ByWindSpd.weather_time
and ByWindSpd.weather_type = "Wind Speed"
left join gl_weather_data ByWaveHeight
on MaxTimes.weather_lat = ByWaveHeight.weather_lat
and MaxTimes.weather_long = ByWaveHeight.weather_long
and MaxTimes.WaveHeightTime = ByWaveHeight.weather_time
and ByWaveHeight.weather_type = "Wave Height"
第一个FROM表是您的主位置列表...通过执行左连接,您将告诉SQL,无论右侧是否有时间戳匹配,都要显示所有位置。
接下来是MaxTimes的PreQuery。可以将此子查询看作是预先收集每个经纬度/经度的最大时间,以及通过MAX报告的每个测量类型的最大时间。)作为FinalColumnName。因此,在每条记录上,它将仅符合这些案例条目中的一个,如果找到,则获取该列的最大时间。
现在,您有了一个位置列表,并在各自的LAT/LONG上联接子查询MaxTimes。现在,MaxTimes查询正在对度量数据执行三次左连接,分别针对每个度量,所以我已经根据它们的用途对它们进行了别名。因此出现了ByWindDir、ByWindSpd和ByWaveHeight。与其他连接一样,左连接基于经纬度,其相应的类型("Wind Direction“、"Wind Speed”或"Wave Height"),并且具有与该类别相同的匹配时间。
这个查询的好处是,如果您的时间到达单个经度/经度位置都是关闭的,即使以秒为单位(即:风向为11:58:02,风速为11:58:05,波高= 11:58:54 ),它仍将遵守每个唯一测量的最大时间。
最后,字段列表...为了把所有的东西都钉在一行上,我得到了显而易见的细节...由于每个表都被联接来表示它自己的“内容”,所以我用列名表示上下文……希望这能帮助你理解这个查询……一次选择一个部分,找出什么链接到什么。
发布于 2011-02-24 19:52:03
尝试:
SELECT l.location_name, l.location_lat, l.location_long,
d.weather_type, d.weather_value, d.weather_unit FROM gl_weather_locations l
LEFT JOIN gl_weather_data d on l.location_lat = d.weather_lat
**order by weather_time desc limit 3**
https://stackoverflow.com/questions/5109666
复制相似问题