1

I am working with SPARQL and having trouble understanding how it actually goes through the input file. For example, I have three orders of the same product from three different businesses. In my query, it works when there are only two purchases to two businesses, but when I add the third of each in, it goes through almost like a loop. It checks one purchase for the first business, two for the second, and three for the third. I am confused on why it would do something like a loop?

Mostly, I am having trouble understanding how SPARQL searches the input file when you press start.

Example of a query would be like

SELECT ?price
WHERE{
      ?test    e:price    ?price;
               e:business ?business.

      ?test2   e:price    ?price2;
               e:business ?business2.
}
user1843804
  • 33
  • 1
  • 5

1 Answers1

1

This isn't anything that's unique to SPARQL. If you were writing in SQL, you'd get the same kind of thing. Your query asks for the value of the variable price for each matching instance of the pattern. Imagine that you have three people and their ages:

:tom   :age 31 .
:dick  :age 32 .
:harry :age 34 .

If you ask "pick a person and a persson and give me the age of the first one", well, there are nine ways to pick two people (since you can pick the same person twice), and that means that you'll have nine answers. E.g., if you run a query like:

select ?p1 ?p2 ?age {
  ?p1 :age ?age
  ?p2 :age ?age2
}

p1     p2     age
-----------------
tom    tom    31
tom    dick   31
tom    harry  31
dick   tom    32
dick   dick   32
dick   harry  32
harry  tom    34
harry  dick   34
harry  harry  34

It's not really clear what you are trying to retrieve, but if you want to ensure that the two instances are distinct, you can add a filter:

select ?p1 ?p2 ?age {
  ?p1 :age ?age
  ?p2 :age ?age2
  filter( ?p1 != ?p2 )
}

p1     p2     age
-----------------
tom    dick   31
tom    harry  31
dick   tom    32
dick   harry  32
harry  tom    34
harry  dick   34

You could also add a filter to impose an ordering on the terms, in which case you could further restrict the number of pairs that you get (just one per each two individuals):

select ?p1 ?p2 ?age {
  ?p1 :age ?age
  ?p2 :age ?age2
  filter( ?p1 < ?p2 )
}

p1     p2     age
-----------------
dick   tom    32
dick   harry  32
harry  tom    34

Without knowing exactly what you are trying to retrieve, it's not really possible to give you anything more than these general guidelines.

Joshua Taylor
  • 84,998
  • 9
  • 154
  • 353