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

时间:2016-08-13 23:43:29

标签: sql yql

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

2 个答案:

答案 0 :(得分:1)

您可以加入子查询,该子查询按照匹配的城市代码的降序返回最后两个日期。您需要加入城市代码和预测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"))

答案 1 :(得分:0)

以下查询为我提供了我想要的内容。

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"
      }
     }
    }
   ]
  }
 }
}