SPARQL:变量必须包含在Group By子句中

发布于 2025-01-28 15:07:45 字数 7926 浏览 4 评论 0原文

对于每个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)

结果:

OFOIDayMintempTime
http://www.example.com/room1foi151.42021-01-01-01 0:00:00:
00 。
​00
http://www.example.com/room1foi259.82021-01-02 16:00:00:00
http://www.example.com/roome.com/room1foi260.2 60.2<代码> 2021-01-02 8:00:00
http://www.example.com/room1foi261.42021-01-01-02 0:00:00

​从子句中删除time将返回正确的行。但是,时间必须包括在内。

理想结果:

OfoiDayMintempTime
http://www.example.com/room1foi149.82021-01-01-01 16:00:00
http://www.example.com/room/room/room1foi259.89.8982021-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

结果:

O2OFOI2Day2val2 val2tx2p2
http://www.example3.com/obs3room100http://www.example3.com/room100foi1482021-01-01-01-01 16:00:00:00:00:00 <“ temp” ^^ http://www.w.w.w3.org/2001/xmlschema#字符串
http://www.example3.com/obs6room1http://www.example3.com/ ROOM1FOI259.82021-01-02 16:00:00“ temp” ^^ http://www.w3.org/2001/xmlschema#string
http://www.example3.com/obs33333room1http://www.example3.com/room1foi1-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/obs33room1http://www.example3.com/ ROOM1FOI1-9.82021-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:

oFoidayminTemptime
http://www.example.com/room1Foi151.42021-01-01 0:00:00
http://www.example.com/room1Foi150.22021-01-01 8:00:00
http://www.example.com/room1Foi149.82021-01-01 16:00:00
http://www.example.com/room1Foi259.82021-01-02 16:00:00
http://www.example.com/room1Foi260.22021-01-02 8:00:00
http://www.example.com/room1Foi261.42021-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:

oFoidayminTemptime
http://www.example.com/room1Foi149.82021-01-01 16:00:00
http://www.example.com/room1Foi259.82021-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:

o2oFoi2day2val2tx2p2
http://www.example3.com/obs3Room100http://www.example3.com/room100Foi1482021-01-01 16:00:00"TEMP"^^http://www.w3.org/2001/XMLSchema#string
http://www.example3.com/obs6Room1http://www.example3.com/room1Foi259.82021-01-02 16:00:00"TEMP"^^http://www.w3.org/2001/XMLSchema#string
http://www.example3.com/obs333Room1http://www.example3.com/room1Foi1-9.82021-01-01 16:00:00"aTEMP"^^http://www.w3.org/2001/XMLSchema#string
http://www.example3.com/obs33Room1http://www.example3.com/room1Foi1-9.82021-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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

再见回来 2025-02-04 15:07:45

添加外部选择似乎可以解决此问题。在时间上使用聚合,例如min()max()avg()sample()等都是汇总时间变量的有效方法。 理解

以帮助

select ?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 ?oFoi2 ?day2 ?p2 ?val2

属性atemp添加
额外 “> http://www.example3.com/room100foi1482021-01-01 16:00:00:00“ temp” ^^ http://www.w.33.org/2001/xmlschema#string
http://www.example3.com/room1foi259.82021-01-01-02 16:00:00:00 ^ http://wwwww.w.w.3.org/2001/xmlschemaunding
href =“ http://www.example3.com/room1foi” rel =“ nofollow noreferrer”> http://www.example3.com/room1foi1-9.82021-01-01-01 7: 59:00“ atemp” ^^ http:// http://www.w3.org/ 2001/xmlschema#string
...... ............

Adding an outer select appears to solve this. Using an aggregate for the time such as min(), max(), avg(), sample(), etc. are all valid approaches for aggregating the time variable. (Adding an extra property aTEMP to help in understanding.)

Query:

select ?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 ?oFoi2 ?day2 ?p2 ?val2

Result:

oFoi2day2val2tx2p2
http://www.example3.com/room100Foi1482021-01-01 16:00:00"TEMP"^^http://www.w3.org/2001/XMLSchema#string
http://www.example3.com/room1Foi259.82021-01-02 16:00:00"TEMP"^^http://www.w3.org/2001/XMLSchema#string
http://www.example3.com/room1Foi1-9.82021-01-01 7:59:00"aTEMP"^^http://www.w3.org/2001/XMLSchema#string
...............
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文