2

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.

user1234SI.
  • 1,812
  • 1
  • 8
  • 22

1 Answers1

1

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
Karam Mohamed
  • 843
  • 1
  • 7
  • 15
  • 1
    I don't get why you need grouping here? I mean, what's wrong with just doing `FILTER(?x < ?y)` resp. if the triple store is picky do `FILTER(str(?x) < str(?y))`? – UninformedUser May 26 '20 at 07:23
  • @UninformedUser the filter only prevents from having the same student in one triplet , like ST1 BOOK1 ST1 , so we will have ST1 BOOK1 ST2 and ST2 BOOK1 ST1 , and to prevent this repetition we cant use only distinct because we dont have one variable , one of the possible methods in this way is to group by the BOOK to make it dinstinct – Karam Mohamed May 26 '20 at 13:34
  • 1
    that is not true. My filter will always ensure that there is only one combination of `(x,y)` because either `x < y` or `y < x` - if you don't believe me, try it – UninformedUser May 26 '20 at 15:19
  • I already tried it , using filter in the case of OP is not enough , he used the filter but altough it gave a repetition , and the solution i gave him was tried and works perfectly , we both tried it and it works , doesnt it work for you ? – Karam Mohamed May 26 '20 at 16:45