如何通过查询来获取总计在SPARQL组(How to get grand total in a sp

2019-10-31 05:51发布

我跟进查询 ,其中schema.org数据库是用来寻找类的儿童人数。 每个等级的答案给孩子的数量。 在我的应用程序所需要的总计所有儿童(即计数每个组的总和),以计算每个组儿童总数的百分比。 我从以前的问题,得到的查询是:

prefix schema:  <http://schema.org/>
prefix rdfs:    <http://www.w3.org/2000/01/rdf-schema#>

select   ?child (count(?grandchild) as ?nGrandchildren) 
from <http://localhost:3030/testDB/data/schemaOrg>
where {
  ?child rdfs:subClassOf schema:Event .
  optional { 
    ?grandchild rdfs:subClassOf ?child
  }
}  group by ?child

这给预期的应答(事件和孩子的数量)。 如何获得总数? 我尝试了嵌套查询为:

prefix schema:  <http://schema.org/>
prefix rdfs:    <http://www.w3.org/2000/01/rdf-schema#>

select   ?child (count(?grandchild) as ?nGrandchildren) 
from <http://localhost:3030/testDB/data/schemaOrg>
where {
  select (count(?grandchild) as ?grandTotal) 
  {?child rdfs:subClassOf schema:Event .
  optional { 
    ?grandchild rdfs:subClassOf ?child
     }
   }
}  group by ?child

但得到了一个答案:“” - > 0。

Answer 1:

此查询使用两个子选择:*第一计算每名儿童孙子*第二返回孙子总数数量

prefix schema:  <http://schema.org/>
prefix rdfs:    <http://www.w3.org/2000/01/rdf-schema#>

select ?child ?nGrandchildren 
(round(?nGrandchildren/?totalGrandchildren * 100) as ?percentageGrandchildren) {

# compute number per child
{
select ?child (count(?grandchild) as ?nGrandchildren) where {
  ?child rdfs:subClassOf schema:Event .
  optional { 
    ?grandchild rdfs:subClassOf ?child
  }
}
group by ?child
}

# compute total number
{
select (count(?grandchild) as ?totalGrandchildren) where {
  ?child rdfs:subClassOf schema:Event .
  optional { 
    ?grandchild rdfs:subClassOf ?child
  }
}
}

}

产量

-----------------------------------------------------------------------------------------------
| child                   | nGrandchildren | percentageGrandchildren                          |
===============================================================================================
| schema:UserInteraction  | 9              | "82"^^<http://www.w3.org/2001/XMLSchema#decimal> |
| schema:FoodEvent        | 0              | "0"^^<http://www.w3.org/2001/XMLSchema#decimal>  |
| schema:MusicEvent       | 0              | "0"^^<http://www.w3.org/2001/XMLSchema#decimal>  |
| schema:PublicationEvent | 2              | "18"^^<http://www.w3.org/2001/XMLSchema#decimal> |
| schema:LiteraryEvent    | 0              | "0"^^<http://www.w3.org/2001/XMLSchema#decimal>  |
| schema:SportsEvent      | 0              | "0"^^<http://www.w3.org/2001/XMLSchema#decimal>  |
| schema:DanceEvent       | 0              | "0"^^<http://www.w3.org/2001/XMLSchema#decimal>  |
| schema:ScreeningEvent   | 0              | "0"^^<http://www.w3.org/2001/XMLSchema#decimal>  |
| schema:DeliveryEvent    | 0              | "0"^^<http://www.w3.org/2001/XMLSchema#decimal>  |
| schema:ExhibitionEvent  | 0              | "0"^^<http://www.w3.org/2001/XMLSchema#decimal>  |
| schema:EducationEvent   | 0              | "0"^^<http://www.w3.org/2001/XMLSchema#decimal>  |
| schema:SaleEvent        | 0              | "0"^^<http://www.w3.org/2001/XMLSchema#decimal>  |
| schema:VisualArtsEvent  | 0              | "0"^^<http://www.w3.org/2001/XMLSchema#decimal>  |
| schema:CourseInstance   | 0              | "0"^^<http://www.w3.org/2001/XMLSchema#decimal>  |
| schema:ChildrensEvent   | 0              | "0"^^<http://www.w3.org/2001/XMLSchema#decimal>  |
| schema:BusinessEvent    | 0              | "0"^^<http://www.w3.org/2001/XMLSchema#decimal>  |
| schema:Festival         | 0              | "0"^^<http://www.w3.org/2001/XMLSchema#decimal>  |
| schema:ComedyEvent      | 0              | "0"^^<http://www.w3.org/2001/XMLSchema#decimal>  |
| schema:TheaterEvent     | 0              | "0"^^<http://www.w3.org/2001/XMLSchema#decimal>  |
| schema:SocialEvent      | 0              | "0"^^<http://www.w3.org/2001/XMLSchema#decimal>  |
-----------------------------------------------------------------------------------------------


文章来源: How to get grand total in a sparql group by query
标签: sparql