SPARQL组由子字符串和平均值组成

时间:2015-07-23 13:09:01

标签: sparql opendata

我正在查询一个大型数据集(每小时记录一次近20年的温度),我宁愿得到摘要,例如:每日气温。

示例查询在此处: http://www.boisvert.me.uk/opendata/sparql_aq+.html?pasteid=hu5rbc7W

PREFIX opensheff: <uri://opensheffield.org/properties#>

select ?time ?temp where {
    ?m opensheff:sensor <uri://opensheffield.org/datagrid/sensors/Weather_Mast/Weather_Mast.ic> ;
       opensheff:rawValue ?temp ;
       <http://purl.oclc.org/NET/ssnx/ssn#endTime> ?time .
  FILTER (str(?time) > "2011-09-24")
}
ORDER BY ASC(?time)

结果如下:

time                temp
"2011-09-24T00:00Z" 12.31
"2011-09-24T01:00Z" 11.68
"2011-09-24T02:00Z" 11.92
"2011-09-24T03:00Z" 11.59

现在我想用部分分组日期字符串,以获得每日平均温度:

time            temp
"2011-09-24"    12.3  # or whatever
"2011-09-23"    11.7
"2011-09-22"    11.9
"2011-09-21"    11.6

那么,我如何按时间分组?

1 个答案:

答案 0 :(得分:3)

最终解决了它。在这里跑:

http://www.boisvert.me.uk/opendata/sparql_aq+.html?pasteid=j8m0Qk6s

代码: PREFIX opensheff:

select ?d AVG(?temp) as ?day_temp
where {
    ?m opensheff:sensor <uri://opensheffield.org/datagrid/sensors/Weather_Mast/Weather_Mast.ic> ;
       opensheff:rawValue ?temp ;
       <http://purl.oclc.org/NET/ssnx/ssn#endTime> ?time .
    BIND( SUBSTR(?time, 1, 10) AS ?d ) .
}
GROUP BY ?d
ORDER BY ASC(?d)

我们使用BIND将新变量设置为所需的子字符串,然后按该变量进行分组和平均就足够了。

相关问题