I am trying to get list of all the authors who have had more than 3 piece of work - DBpedia Sparql

家住魔仙堡 提交于 2019-12-02 07:13:40

问题


I am trying to get list of all the authors who have had 3 or more piece of work done (in DBpedia).

my example can be run on : http://dbpedia.org/sparql

base code

select (count(?work) as ?totalWork), ?author
Where
{
  ?work dbo:author ?author.
}
GROUP BY ?author

I get each authors total amount of piece of work done. But when I try to filter to show only list of author that have more than 3 piece of work. I get error:

I tried HAVING keyword or using FILTER keyword.

Using Filter

select (count(?work) as ?tw), ?author
Where
{
  ?work dbo:author ?author.
  FILTER (?work > 3).
}
GROUP BY ?author

error: Virtuoso 22023 Error VECDT: SR066: Unsupported case in CONVERT (INTEGER -> IRI_ID)

Using HAVING keyword

select (count(?work) as ?tw), ?author
Where
{
  ?work dbo:author ?author.
}
GROUP BY ?author
HAVING (?tw > 3)

Virtuoso 37000 Error SP031: SPARQL compiler: Variable ?tw is used in the result set outside aggregate and not mentioned in GROUP BY clause

回答1:


Using HAVING is correct, but there is a limitation in SPARQL with indirectly referring to aggregates.

This one works:

SELECT (count(?work) as ?tw) ?author
WHERE
{
  ?work dbo:author ?author.
}
GROUP BY ?author
HAVING (count(?work) > 3)



回答2:


HAVING (?tw > 3) is correct SPARQL. HAVING filters after assignments due to SELECT, so ?tw is visible, and before projection.

(prefix ((dbo: <http://purl.org/dc/elements/1.1/>))
    (project (?tw ?author)
      (filter (> ?tw 3)
        (extend ((?tw ?.0))
          (group (?author) ((?.0 (count ?work)))
            (bgp (triple ?work dbo:author ?author)))))))

where ?.0 is the assignment of count.



来源:https://stackoverflow.com/questions/43617385/i-am-trying-to-get-list-of-all-the-authors-who-have-had-more-than-3-piece-of-wor

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!