如何通过查询获取sparql组中的总计

时间:2017-11-18 17:35:53

标签: sparql

我跟进query,其中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

但得到了一个答案:&#34; &#34; - &GT; 0.

1 个答案:

答案 0 :(得分:4)

此查询使用两个子SELECT: *第一个计算每个孩子的孙子数 *第二个返回孙子的总数

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>  |
-----------------------------------------------------------------------------------------------