sparqlrdf4j

How to display distinct pair of students?


How can I display distinct pair of students if I have a relation in my graph like this:

:ST1 :read :BOOK1,:BOOK2,:BOOK3.
:ST2 :read :BOOK1,:BOOK2.
:ST3 :read :BOOK2.

And in my select I have something like this:

SELECT DISTINCT ?x ?book ?y
{
   ?x :read ?book.
   ?book ^:read ?y.
   FILTER(?x != ?y).
}

But with my graph data the output will be :

 :ST1 :BOOK1 :ST2
 :ST2 :BOOK1 :ST1 ... etc

And I want only the :ST1 :BOOK1 :ST2 relation to be displayed.


Solution

  • Your problem is that you were putting DISTINCT where it cannot be applied in one variable only when you display more than one , you can make this instead :

    SELECT (SAMPLE(?x) as ?xx) ?book (SAMPLE(?y) as ?yy)
     {
       ?y :read ?book.
       ?book ^:read ?x.
       FILTER(?x != ?y).
    }
    GROUP BY ?book
    

    Output :

    :ST1 :BOOK1 :ST2