1

I have the current SPARQL code:

SELECT ?currentQid ?propQid ?targetQid WHERE
{
  VALUES ?current {wd:Q5 wd:Q6 wd:Q7 wd:Q8 wd:Q9}
  VALUES ?prop {wdt:P31 wdt:P279 wdt:P361}
  BIND (STRAFTER(STR(?current), STR(wd:)) AS ?currentQid)
  BIND (STRAFTER(STR(?prop), STR(wdt:)) AS ?propQid)
  BIND (STRAFTER(STR(?target), STR(wd:)) AS ?targetQid)
  ?current ?prop ?target.
}
ORDER BY ASC (?currentQid)

(w.wiki/3Gn)

Which outputs

currentQid  propQid  targetQid
    Q5        P31    Q55983715
    Q5        P279   Q154954
    Q5        P279   Q215627
    Q5        P361   Q1156970
    Q8        P31    Q331769
    Q8        P31    Q60539479
    Q8        P279   Q16748867

However, I wanted to also have the current index of the value, so I can correctly attribute the values to the array's item when I get the JSON.

How could I get the following table?

index  currentQid  propQid  targetQid
  0        Q5        P31    Q55983715
  0        Q5        P279   Q154954
  0        Q5        P279   Q215627
  0        Q5        P361   Q1156970
  3        Q8        P31    Q331769
  3        Q8        P31    Q60539479
  3        Q8        P279   Q16748867

Thanks!

2

VALUES are unordered; SPARQL list syntax is also not allowed in VALUES. The simplest option is explicit ordering:

SELECT ?index ?currentQid ?propQid ?targetQid WHERE
{ 
  VALUES (?current ?index) {(wd:Q5 0) (wd:Q6 1) (wd:Q7 2) (wd:Q8 3) (wd:Q9 4)}
  VALUES ?prop {wdt:P31 wdt:P279 wdt:P361}
  ?current ?prop ?target. 
  BIND (STRAFTER(STR(?current), STR(wd:)) AS ?currentQid)
  BIND (STRAFTER(STR(?prop), STR(wdt:)) AS ?propQid)
  BIND (STRAFTER(STR(?target), STR(wd:)) AS ?targetQid)
}
ORDER BY ASC (xsd:integer(STRAFTER(?currentQid, 'Q')))

Try it

  • Thank you very much! That's exactly what I needed. – Henrique Bruno Apr 24 at 18:54

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.