The Knowledge Graph Conference Icon
The Knowledge Graph Conference
  • 🏠Home
  • 📅Events
  • 👤Members
  • 🔵Announcements
  • 🔵Ask
  • 🔵Ask The Ontologists
  • 🔵Events
  • 🔵Jobs
  • 🔵Promotions
  • 🔵Share
Powered by Tightknit
Ask
Ask

Counting Union of Two Restrictions in SPARQL Queries

Avatar of Leia D.Leia D.
·Jan 23, 2023 09:09 PM

SPARQL question— is it possible to do a COUNT command on the union of two restrictions? Here is the query syntax.

10 comments

· Sorted by Oldest
    • Avatar of Ricardo U.
      Ricardo U.
      ·

      Hi, sure, that should work. Do you get an error?

      👍1
    • Avatar of Leia D.
      Leia D.
      ·

      No. The result isn’t giving me what I expect. One query solo gives me 156, the other solo 190. When I run them as a Union, I get 156. Given that, I feel like I should get a different number.

    • Avatar of Ricardo U.
      Ricardo U.
      ·

      I do not see the closing brackets, could be the source of error. My guess is that ?x is always bound in the first UNION and thus you would need to reformulate to use OPTIONAL instead of UNION and move ?x rdf:type ?restriction outside

    • Avatar of Nikos T.
      Nikos T.
      ·

      If you want a simple count you could remove the union and change the last triple to something like this:

      ?restriction owl:someValuesFrom ?itemOneOrItemTwo .
      FILTER(?itemOneOrItemTwo IN (gfo:itemOne, gfo:itemTwo))

      This way you can add/remove values as needed.

      ✅1
    • Avatar of Leia D.
      Leia D.
      ·

      Thank you! I’ll try this.

    • Avatar of Leia D.
      Leia D.
      ·

      Unfortunately, this didn’t work but I think it’s because of the issue on our end. The classes in question have a class/subclass relationship. So I think I’ll have to try something else… but I think this works if classes are sibling classes.

    • Avatar of Valerio C.
      Valerio C.
      ·

      Why are you trying to use this query, rather than simply:

      SELECT (COUNT(DISTINCT ?x) AS ?nox)
      WHERE {
      VALUES ?topClass {gfo:itemOne gfo:itemTwo}
      ?x gfo:involves/rdf:type ?topClass
      }

      Or also:

      SELECT (COUNT(DISTINCT ?x) AS ?nox)
      WHERE {
      {?x gfo:involves/rdf:type gfo:itemOne}
      UNION
      {?x gfo:involves/rdf:type gfo:itemTwo}
      }

    • Avatar of Leia D.
      Leia D.
      ·

      Thank you. I’ll try this next time…

    • Avatar of Valerio C.
      Valerio C.
      ·

      Did you end up finding another solution or just gave up?

    • Avatar of Leia D.
      Leia D.
      ·

      I found a workaround.