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

Limit query result to the first of each group

我正在使用 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 个频道下(即每个城市的第一个)。如何将结果限制为这两个?

您可以加​​入一个子查询,其中 return 匹配城市代码的最后两个日期按降序排列。您需要在城市代码和预测 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"))

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

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"

我不知道为什么 Yahoo 的主要 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"
      }
     }
    }
   ]
  }
 }
}