1

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!

user7665853
  • 195
  • 1
  • 15

1 Answers1

3

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'

  • Thank you so much. Simple solution it was! – user7665853 Jul 27 '22 at 16:03
  • If I may, one additional question. How to fecth the label of properties? I use `?propertyLable` and `SERVICE wikibase:label { bd:serviceParam wikibase:language "[AUTO_LANGUAGE], en". }` but it returned URIs instead of labels. – user7665853 Jul 27 '22 at 16:13
  • It looks like: `SELECT DISTINCT ?item ?itemLabel ?property ?propertyLabel WHERE { VALUES ?property { wdt:P170 wdt:P50 } ?item ?property wd:Q254 . SERVICE wikibase:label { bd:serviceParam wikibase:language "[AUTO_LANGUAGE], en, de". } } LIMIT 10` – user7665853 Jul 27 '22 at 16:14
  • @user7665853: I think it’s best to ask this as its own question. – Stefan - brox IT-Solutions Jul 28 '22 at 08:17
  • 1
    Ok, thanks anyway. I found one solution here which works for me: https://stackoverflow.com/questions/56486888/how-to-get-property-labels-from-wikidata-using-sparql – user7665853 Jul 28 '22 at 08:27