hornet
hornet

Reputation: 1

SPARQL same indi

Can SPARQL be used to find subjects having identical objects for a given predicate. Considering a class Variable with data property as Variable--hasvalue-->Integer, if there are five instances such as

  1. a ----hasvalue------> 2
  2. b ----hasvalue------> 1
  3. c ----hasvalue------> 2
  4. d ----hasvalue------> 0
  5. e ----hasvalue------> 1

How to extract a and c has same value whereas b and e has same value. Group option works in grouping as above, but is it possible to extract subjects corresponding to each grouped objects.

Upvotes: 0

Views: 41

Answers (1)

Joshua Taylor
Joshua Taylor

Reputation: 85883

It's always easier to write example code if you provide sample data to work with. Please provide sample data in the future. Your suggested data looks like this:

@prefix : <urn:ex:>

:a :hasValue 2 .
:b :hasValue 1 .
:c :hasValue 2 .
:d :hasValue 0 .
:e :hasValue 1 .

You can use a query with group by and group_concat to concatenate the variables together for each distinct value:

prefix : <urn:ex:>

select ?value (group_concat(?variable) as ?variables) {
  ?variable :hasValue ?value 
}
group by ?value
-------------------------------
| value | variables           |
===============================
| 2     | "urn:ex:c urn:ex:a" |
| 1     | "urn:ex:e urn:ex:b" |
| 0     | "urn:ex:d"          |
-------------------------------

Upvotes: 1

Related Questions