简体   繁体   中英

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

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 (eg http://www.wikidata.org/prop/direct/P50 )

Thank you

You could use VALUES instead of UNION :

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

}

Result:

带有“属性”列的结果表

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM