SPARQL 按子串和平均值分组

SPARQL group by a substring and average

我正在查询一个大型数据集(近 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

那么,如何按 ?time 的子字符串分组?

终于解决了。 运行 这里:

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

代码: 前缀 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 为所需的子字符串设置一个新变量,然后根据该变量进行分组和平均就足够简单了。