I am trying to get list of all the authors who hav

2019-03-02 18:19发布

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

3条回答
Luminary・发光体
2楼-- · 2019-03-02 18:30

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)
查看更多
兄弟一词,经得起流年.
3楼-- · 2019-03-02 18:39

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.

查看更多
啃猪蹄的小仙女
4楼-- · 2019-03-02 18:47

Using HAVING

In addition to AKSW's answer — https://github.com/openlink/virtuoso-opensource/issues/254.

Using FILTER

You should write something like that:

SELECT *
WHERE
{
  {
  SELECT ?author (COUNT(?work) AS ?tw)
  WHERE
  {
    ?work dbo:author ?author.
  } GROUP BY ?author
  } FILTER ( ?tw > 3 )
}
查看更多
登录 后发表回答