将查询结果限制在每个组中的第一个

问题描述:

我使用YQL获取当今多个城市的高温和低温。以下查询将不必要地return a 10-day forecast for each city将查询结果限制在每个组中的第一个

select item.forecast.high, item.forecast.low from weather.forecast 
where woeid in (select woeid from geo.places(1) where text in ("ushuaia, ag", "dallas, tx")) 

我需要的是在结果的通道0和10(即每个城市的第一个)下找到的。我怎样才能将结果限制在这两个?

以下查询给了我我想要的。

select item.forecast.high, item.forecast.low from weather.forecast 
where woeid in (select woeid from geo.places(1) where text in ("ushuaia, ag", "dallas, tx")) 
and item.forecast.date="14 Aug 2016" 

我不知道为什么雅虎的主要YQL控制台(在这个问题链接)不与天气查询,这一刻的工作,但你仍然可以尝试在其weather API page查询。

结果:

{ 
"query": { 
    "count": 2, 
    "created": "2016-08-14T20:13:35Z", 
    "lang": "en-US", 
    "results": { 
    "channel": [ 
    { 
    "item": { 
     "forecast": { 
     "high": "25", 
     "low": "15" 
     } 
    } 
    }, 
    { 
    "item": { 
     "forecast": { 
     "high": "88", 
     "low": "75" 
     } 
    } 
    } 
    ] 
    } 
} 
} 

您可以加入一个子查询,该查询以匹配城市代码的降序返回最后两个日期。您需要加入城市代码和预测ID。

下面的SQL是T-SQL,但我想在YQL中会有类似的东西。

select item.forecast.high, 
    item.forecast.low 
from weather.forecast forecast 
JOIN (SELECT TOP 2 id, woeid, date 
    FROM weather.forecast 
    ORDER BY date DESC) dates ON dates.woeid = forecast.woeid 
          AND dates.id = forecast.id 
where woeid in (select woeid 
       from geo.places(1) 
       where text in ("ushuaia, ag", "dallas, tx")) 
+0

感谢您尝试帮助。作为全新的数据库,我不太明白你的答案。有一天我会回来的,但现在幸运的是,我找到了一个更简单的方法。 – bongbang