SPARQL:变量必须包含在Group By子句中
对于每个sosa:featurefintest
(房间),从该房间的相关传感器中获得最低温度。有100个房间。每个房间都有3个传感器。时间表是一年。
目标:查询以从传感器组中选择每房间最低温度加上温度发生的时间的时间。
示例数据(N3):
prefix rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns>
prefix rdfs: <http://www.w3.org/2000/01/rdf-schema#>
prefix sosa: <http://www.w3.org/ns/sosa/>
prefix xsd: <http://www.w3.org/2001/XMLSchema#>
prefix ex: <http://www.example.com/>
# Room FOIs
ex:room1Foi a sosa:FeatureOfInterest .
# ...
ex:room100Foi a sosa:FeatureOfInterest .
# Room 1 sensor observations 1/1/2021
ex:obs1Room1 a sosa:Observation .
sosa:hasFeatureOfInterest ex:room1Foi ;
sosa:resultTime "2021-01-01T00:00:00"^^xsd:dateTime ;
sosa:observedProperty "TEMP"^^xsd:string ;
sosa:hasSimpleResult "51.4"^^xsd:decimal .
ex:obs2Room1 a sosa:Observation .
sosa:hasFeatureOfInterest ex:room1Foi .
sosa:resultTime "2021-01-01T08:00:00"^^xsd:dateTime ;
sosa:observedProperty "TEMP"^^xsd:string ;
sosa:hasSimpleResult "50.2"^^xsd:decimal .
ex:obs3Room1 a sosa:Observation .
sosa:hasFeatureOfInterest ex:room1Foi .
sosa:resultTime "2021-01-01T:16:00"^^xsd:dateTime ;
sosa:observedProperty "TEMP"^^xsd:string ;
sosa:hasSimpleResult "49.8"^^xsd:decimal .
# Room 1 sensor observations 1/2/2021
ex:obs4Room1 a sosa:Observation .
sosa:hasFeatureOfInterest ex:room1Foi ;
sosa:resultTime "2021-01-02T00:00:00"^^xsd:dateTime ;
sosa:observedProperty "TEMP"^^xsd:string ;
sosa:hasSimpleResult "61.4"^^xsd:decimal .
ex:obs5Room1 a sosa:Observation .
sosa:hasFeatureOfInterest ex:room1Foi .
sosa:resultTime "2021-01-02T08:00:00"^^xsd:dateTime ;
sosa:observedProperty "TEMP"^^xsd:string ;
sosa:hasSimpleResult "60.2"^^xsd:decimal .
ex:obs6Room1 a sosa:Observation .
sosa:hasFeatureOfInterest ex:room1Foi .
sosa:resultTime "2021-01-02T:16:00"^^xsd:dateTime ;
sosa:observedProperty "TEMP"^^xsd:string ;
sosa:hasSimpleResult "59.8"^^xsd:decimal .
# ...
# Room 100 sensor observations 1/1/2021
ex:obs1Room100 a sosa:Observation .
sosa:hasFeatureOfInterest ex:room100Foi ;
sosa:resultTime "2021-01-01T00:00:00"^^xsd:dateTime ;
sosa:observedProperty "TEMP"^^xsd:string ;
sosa:hasSimpleResult "50.7"^^xsd:decimal .
ex:obs2Room100 a sosa:Observation .
sosa:hasFeatureOfInterest ex:room100Foi .
sosa:resultTime "2021-01-01T08:00:00"^^xsd:dateTime ;
sosa:observedProperty "TEMP"^^xsd:string ;
sosa:hasSimpleResult "51.6"^^xsd:decimal .
ex:obs3Room100 a sosa:Observation .
sosa:hasFeatureOfInterest ex:room100Foi .
sosa:resultTime "2021-01-01T:16:00"^^xsd:dateTime ;
sosa:observedProperty "TEMP"^^xsd:string ;
sosa:hasSimpleResult "48.0"^^xsd:decimal .
# Room 1 sensor observations 1/2/2021
# ...
一次尝试:
prefix rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns>
prefix rdfs: <http://www.w3.org/2000/01/rdf-schema#>
prefix sosa: <http://www.w3.org/ns/sosa/>
prefix xsd: <http://www.w3.org/2001/XMLSchema#>
prefix ex: <http://www.example1.com/>
select ?oFoi ?day min(?val) as ?minTemp ?time where {
{select ?f where {
?f a sosa:FeatureOfInterest .
}}
?o a sosa:Observation ;
sosa:hasFeatureOfInterest ?oFoi ;
sosa:resultTime ?time ;
sosa:observedProperty ?p ;
sosa:hasSimpleResult ?val .
filter(?oFoi = ?f) .
bind(day(?t) as ?day) .
} group by ?oFoi ?day ?time
order by desc(?oFoi) asc(?day)
结果:
OFOI | Day | Mintemp | Time |
---|---|---|---|
http://www.example.com/room1foi | 1 | 51.4 | 2021-01-01-01 0:00:00: |
00 。 | | | |
| | | 00 |
http://www.example.com/room1foi | 2 | 59.8 | 2021-01-02 16:00:00:00 |
http://www.example.com/roome.com/room1foi | 2 | 60.2 60.2 | <代码> 2021-01-02 8:00:00 |
http://www.example.com/room1foi | 2 | 61.4 | 2021-01-01-02 0:00:00 |
。 | | |
从子句中删除time
将返回正确的行。但是,时间
必须包括在内。
理想结果:
Ofoi | Day | Mintemp | Time |
---|---|---|---|
http://www.example.com/room1foi | 1 | 49.8 | 2021-01-01-01 16:00:00 |
http://www.example.com/room/room/room1foi | 2 | 59.89.898 | 2021-01-02 16:00:00 |
... | ... | ... |
|
更新: 当同一天两次的温度相同(包括两个结果)时,这越来越近,但仍在包含多个结果:
select ?o2 ?oFoi2 ?day2 ?val2 sample(?t2) as ?tx2 ?p2 where {
?o2 a sosa:Observation ;
sosa:hasFeatureOfInterest ?oFoi2 ;
sosa:resultTime ?t2 ;
sosa:observedProperty ?p2 ;
sosa:hasSimpleResult ?val2 .
bind(day(?t2) as ?day2) .
filter(?oFoi2 = ?oFoi) .
filter(?day2 = ?day) .
filter(?val2 = ?vx) .
{select ?oFoi ?day min(?val) as ?vx where {
{select ?f where {
?f a sosa:FeatureOfInterest .
}}
?o a sosa:Observation ;
sosa:hasFeatureOfInterest ?oFoi ;
sosa:resultTime ?t ;
sosa:observedProperty ?p ;
sosa:hasSimpleResult ?val .
filter(?oFoi = ?f) .
bind(day(?t) as ?day) .
} group by ?oFoi ?day
order by desc(?oFoi) asc(?day)
}
} group by ?o2 ?oFoi2 ?day2 ?p2 ?val2
结果:
O2 | OFOI2 | Day2 | val2 val2 | tx2 | p2 |
---|---|---|---|---|---|
http://www.example3.com/obs3room100 | http://www.example3.com/room100foi | 1 | 48 | 2021-01-01-01-01 16:00:00:00:00:00 < | “ temp” ^^ http://www.w.w.w3.org/2001/xmlschema#字符串 |
http://www.example3.com/obs6room1 | http://www.example3.com/ ROOM1FOI | 2 | 59.8 | 2021-01-02 16:00:00 | “ temp” ^^ http://www.w3.org/2001/xmlschema#string |
http://www.example3.com/obs33333room1 | http://www.example3.com/room1foi | 1 | -9.8
| 2021-01-01-01 16:00:00:00 < /code> | “ atemp” ^^ http://www.www.w.w.w.33.org/2001/xmlschema#字符串 |
http://www.example3.com/obs33room1 | http://www.example3.com/ ROOM1FOI | 1 | -9.8 | 2021-01-01 7:59:00 | “ atemp” ^^ http://www.w3.org/2001/xmlschema#string |
... ... | ... ... ... | ... ... ... | ... | ... ... | org/2001/ xmlschema .. |
糟糕:?o2
是不必要的,并从上面的查询中将其删除为正确的解决方案。
For every sosa:FeatureOfInterest
(room), get the lowest temperature per day from the associated sensors for that room. There are 100 rooms. Each room has 3 sensors. The timeframe is one year.
Goal: Query to select lowest temperature per day per room from group of sensors plus time of day when the temperature occurred.
Example data (N3):
prefix rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns>
prefix rdfs: <http://www.w3.org/2000/01/rdf-schema#>
prefix sosa: <http://www.w3.org/ns/sosa/>
prefix xsd: <http://www.w3.org/2001/XMLSchema#>
prefix ex: <http://www.example.com/>
# Room FOIs
ex:room1Foi a sosa:FeatureOfInterest .
# ...
ex:room100Foi a sosa:FeatureOfInterest .
# Room 1 sensor observations 1/1/2021
ex:obs1Room1 a sosa:Observation .
sosa:hasFeatureOfInterest ex:room1Foi ;
sosa:resultTime "2021-01-01T00:00:00"^^xsd:dateTime ;
sosa:observedProperty "TEMP"^^xsd:string ;
sosa:hasSimpleResult "51.4"^^xsd:decimal .
ex:obs2Room1 a sosa:Observation .
sosa:hasFeatureOfInterest ex:room1Foi .
sosa:resultTime "2021-01-01T08:00:00"^^xsd:dateTime ;
sosa:observedProperty "TEMP"^^xsd:string ;
sosa:hasSimpleResult "50.2"^^xsd:decimal .
ex:obs3Room1 a sosa:Observation .
sosa:hasFeatureOfInterest ex:room1Foi .
sosa:resultTime "2021-01-01T:16:00"^^xsd:dateTime ;
sosa:observedProperty "TEMP"^^xsd:string ;
sosa:hasSimpleResult "49.8"^^xsd:decimal .
# Room 1 sensor observations 1/2/2021
ex:obs4Room1 a sosa:Observation .
sosa:hasFeatureOfInterest ex:room1Foi ;
sosa:resultTime "2021-01-02T00:00:00"^^xsd:dateTime ;
sosa:observedProperty "TEMP"^^xsd:string ;
sosa:hasSimpleResult "61.4"^^xsd:decimal .
ex:obs5Room1 a sosa:Observation .
sosa:hasFeatureOfInterest ex:room1Foi .
sosa:resultTime "2021-01-02T08:00:00"^^xsd:dateTime ;
sosa:observedProperty "TEMP"^^xsd:string ;
sosa:hasSimpleResult "60.2"^^xsd:decimal .
ex:obs6Room1 a sosa:Observation .
sosa:hasFeatureOfInterest ex:room1Foi .
sosa:resultTime "2021-01-02T:16:00"^^xsd:dateTime ;
sosa:observedProperty "TEMP"^^xsd:string ;
sosa:hasSimpleResult "59.8"^^xsd:decimal .
# ...
# Room 100 sensor observations 1/1/2021
ex:obs1Room100 a sosa:Observation .
sosa:hasFeatureOfInterest ex:room100Foi ;
sosa:resultTime "2021-01-01T00:00:00"^^xsd:dateTime ;
sosa:observedProperty "TEMP"^^xsd:string ;
sosa:hasSimpleResult "50.7"^^xsd:decimal .
ex:obs2Room100 a sosa:Observation .
sosa:hasFeatureOfInterest ex:room100Foi .
sosa:resultTime "2021-01-01T08:00:00"^^xsd:dateTime ;
sosa:observedProperty "TEMP"^^xsd:string ;
sosa:hasSimpleResult "51.6"^^xsd:decimal .
ex:obs3Room100 a sosa:Observation .
sosa:hasFeatureOfInterest ex:room100Foi .
sosa:resultTime "2021-01-01T:16:00"^^xsd:dateTime ;
sosa:observedProperty "TEMP"^^xsd:string ;
sosa:hasSimpleResult "48.0"^^xsd:decimal .
# Room 1 sensor observations 1/2/2021
# ...
One attempt:
prefix rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns>
prefix rdfs: <http://www.w3.org/2000/01/rdf-schema#>
prefix sosa: <http://www.w3.org/ns/sosa/>
prefix xsd: <http://www.w3.org/2001/XMLSchema#>
prefix ex: <http://www.example1.com/>
select ?oFoi ?day min(?val) as ?minTemp ?time where {
{select ?f where {
?f a sosa:FeatureOfInterest .
}}
?o a sosa:Observation ;
sosa:hasFeatureOfInterest ?oFoi ;
sosa:resultTime ?time ;
sosa:observedProperty ?p ;
sosa:hasSimpleResult ?val .
filter(?oFoi = ?f) .
bind(day(?t) as ?day) .
} group by ?oFoi ?day ?time
order by desc(?oFoi) asc(?day)
Result:
oFoi | day | minTemp | time |
---|---|---|---|
http://www.example.com/room1Foi | 1 | 51.4 | 2021-01-01 0:00:00 |
http://www.example.com/room1Foi | 1 | 50.2 | 2021-01-01 8:00:00 |
http://www.example.com/room1Foi | 1 | 49.8 | 2021-01-01 16:00:00 |
http://www.example.com/room1Foi | 2 | 59.8 | 2021-01-02 16:00:00 |
http://www.example.com/room1Foi | 2 | 60.2 | 2021-01-02 8:00:00 |
http://www.example.com/room1Foi | 2 | 61.4 | 2021-01-02 0:00:00 |
... | ... | ... | ... |
This doesn't work because ?time
must be included in the group by clause. Removing ?time
from group by clause will return the correct rows. However, ?time
is necessary to be included.
Ideal result:
oFoi | day | minTemp | time |
---|---|---|---|
http://www.example.com/room1Foi | 1 | 49.8 | 2021-01-01 16:00:00 |
http://www.example.com/room1Foi | 2 | 59.8 | 2021-01-02 16:00:00 |
... | ... | ... | ... |
Update:
This gets closer but still is including multiple results when two times in the same day have the same temperature (both results included):
select ?o2 ?oFoi2 ?day2 ?val2 sample(?t2) as ?tx2 ?p2 where {
?o2 a sosa:Observation ;
sosa:hasFeatureOfInterest ?oFoi2 ;
sosa:resultTime ?t2 ;
sosa:observedProperty ?p2 ;
sosa:hasSimpleResult ?val2 .
bind(day(?t2) as ?day2) .
filter(?oFoi2 = ?oFoi) .
filter(?day2 = ?day) .
filter(?val2 = ?vx) .
{select ?oFoi ?day min(?val) as ?vx where {
{select ?f where {
?f a sosa:FeatureOfInterest .
}}
?o a sosa:Observation ;
sosa:hasFeatureOfInterest ?oFoi ;
sosa:resultTime ?t ;
sosa:observedProperty ?p ;
sosa:hasSimpleResult ?val .
filter(?oFoi = ?f) .
bind(day(?t) as ?day) .
} group by ?oFoi ?day
order by desc(?oFoi) asc(?day)
}
} group by ?o2 ?oFoi2 ?day2 ?p2 ?val2
Result:
o2 | oFoi2 | day2 | val2 | tx2 | p2 |
---|---|---|---|---|---|
http://www.example3.com/obs3Room100 | http://www.example3.com/room100Foi | 1 | 48 | 2021-01-01 16:00:00 | "TEMP"^^http://www.w3.org/2001/XMLSchema#string |
http://www.example3.com/obs6Room1 | http://www.example3.com/room1Foi | 2 | 59.8 | 2021-01-02 16:00:00 | "TEMP"^^http://www.w3.org/2001/XMLSchema#string |
http://www.example3.com/obs333Room1 | http://www.example3.com/room1Foi | 1 | -9.8 | 2021-01-01 16:00:00 | "aTEMP"^^http://www.w3.org/2001/XMLSchema#string |
http://www.example3.com/obs33Room1 | http://www.example3.com/room1Foi | 1 | -9.8 | 2021-01-01 7:59:00 | "aTEMP"^^http://www.w3.org/2001/XMLSchema#string |
... | ... | ... | ... | ... | ... |
Oops: ?o2
is unnecessary and removing it from the above query results in the correct solution.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
添加外部
选择
似乎可以解决此问题。在时间上使用聚合,例如min()
,max()
,avg()
,sample()
等都是汇总时间变量的有效方法。 理解以帮助
(
2021-01-01 16:00:00:00
2021-01-01-02 16:00:00:
2021-01-01-01 7: 59:00
Adding an outer
select
appears to solve this. Using an aggregate for the time such asmin()
,max()
,avg()
,sample()
, etc. are all valid approaches for aggregating the time variable. (Adding an extra propertyaTEMP
to help in understanding.)Query:
Result:
2021-01-01 16:00:00
2021-01-02 16:00:00
2021-01-01 7:59:00