简体   繁体   English

SPARQL中的GROUP BY?

[英]GROUP BY in SPARQL?

Assume that I uses the FOAF ontology. 假设我使用FOAF本体。 I want to return the name and the mbox for each person. 我想为每个人返回namembox I use the following query: 我使用以下查询:

PREFIX foaf: <http://xmlns.com/foaf/0.1/>
SELECT DISTINCT ?name ?email
WHERE {
  ?person foaf:name ?name.
  ?person foaf:mbox ?email.
}

The result of the join is a set of rows: ?person , ?name , ?email . 连接的结果是一组行: ?person?name?email This query is returning the ?name and ?email . 此查询返回?name?email Note that in some of the ?person may have multiple mailboxes, so in the returned set, a ?name row may appear multiple times, once for each mailbox. 请注意,在某些?person可能有多个邮箱,因此在返回的集中, ?name行可能会多次出现,每个邮箱一次。

Is there a solution to make a GROUP BY person ?name ? 是否有解决方案来制作GROUP BY person ?name

You can group by person but then you need an aggregation for ?name and ?email 您可以按人分组,但是您需要聚合?name?email

PREFIX foaf: <http://xmlns.com/foaf/0.1/>
SELECT (sample(?name) AS ?name2) (sample(?email) as ?email2)
WHERE {
  ?person foaf:name ?name.
  ?person foaf:mbox ?email.
} GROUP BY ?person

SAMPLE picks one possible from the group for each ?person . SAMPLE为每个?person选择一个可能的组。

or maybe 或者可能

SELECT (group_concat(?name) AS ?names)

(except that's a string). (除了那是一个字符串)。

It may be easier work with 它可能更容易使用

PREFIX foaf: <http://xmlns.com/foaf/0.1/>
SELECT ?name ?email
WHERE {
  ?person foaf:name ?name.
  ?person foaf:mbox ?email.
} 
ORDER BY ?person ?name ?email

and process the results in your application where you know the incoming results have all the entries for one person is a single section of the results. 并在您的应用程序中处理结果,您知道传入的结果包含一个人的所有条目是结果的单个部分。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM