Search code examples
rdfsparqlsesame

Counting with SPARQL: how to get results in one row?


Trying to get three (or more count result in the same SPARQL query:

SELECT
  ?cs
  ?bs
  ?ws
WHERE {
  { SELECT (COUNT(?c) AS ?cs) WHERE { ?c a :OneThing . }}
  UNION
  { SELECT (COUNT(?b) AS ?bs) WHERE { ?b a :AnotherThing . } }
  UNION
  { SELECT (COUNT(?w) AS ?ws) WHERE { ?w a :ThirdThing . } }
}

However, it would be much better if there were just one row with the results rather than three (and the counts on the diagonal). Tried different expressions to no avail.

Can this be done efficiently with SPARQL? I feel like I am missing something obvious...


Solution

  • Does this do the trick?

    SELECT (COUNT(?c) AS ?cs) (COUNT(?b) AS ?bs) (COUNT(?w) AS ?ws)
    WHERE {
        { ?c a :OneThing . }
        UNION
        { ?b a :AnotherThing . }
        UNION
        { ?w a :ThirdThing . }
    }