How to return already specified properties for UNION in the SPARQL query result (as well as variables)?

Viewed 36

Is there a (good) way to return already specified properties for UNION in the SPARQL query result (as well as variables)?

Any endpoint is fine but Wikidata is used as an example below (https://query.wikidata.org/sparql). For example,

SELECT DISTINCT ?item ?person
WHERE {
 {? item wdt:P170 ?person . }
 UNION { ?item wdt:P50 ?person . }
}

This returns something like:

| ?item | ?person |

| http://www.wikidata.org/entity/Q51136119 | http://www.wikidata.org/entity/Q736847 |

| http://www.wikidata.org/entity/Q51136131 | http://www.wikidata.org/entity/Q736847 |


What I need is to get wdt:170 and wdt:P50 in the results, so I can see what properties are used for each relation:

| ?item | ?property | ?person |

| http://www.wikidata.org/entity/Q51136119 | wdt:170 | http://www.wikidata.org/entity/Q736847 |

| http://www.wikidata.org/entity/Q51136131 | wdt:P50 | http://www.wikidata.org/entity/Q736847 |


Note

The example is simplified. There is only one UNION and two results, but there will be more UNIONs, so that it is important to know all used properties.

No problem if the property part could be full URI (e.g. http://www.wikidata.org/prop/direct/P50)

Thank you!

1 Answers

You could use VALUES instead of UNION:

SELECT DISTINCT ?item ?property ?person
WHERE {
  
  VALUES ?property {
    wdt:P170 
    wdt:P50
  }
  
  ?item ?property ?person .

}

Result:

result table with a column for 'property'

Related