Recover the “original” order

我只是一个虾纸丫 提交于 2019-12-12 10:19:50

问题


I am trying to recover the cast list for movies from wikidata. My sparql query for Dr. No is as follows:

SELECT ?actor ?actorLabel WHERE {
  ?movie wdt:P161 ?actor .
  SERVICE wikibase:label { bd:serviceParam wikibase:language "en". }
  FILTER(?movie = wd:Q102754)
}
LIMIT 1000

I can try it out at query.wikidata.org but the results are not in the order that I want. It gives 'Sean Connery', 'Zena Marshall', 'Ursula Andress'.

The database has the data in the required order as you can see from https://www.wikidata.org/wiki/Q102754 includes the cast list in order (Sean Connery, Ursula Andress, Joseph Wiseman). Generally the cast list is given in billing order and it is that that I want to recover.


回答1:


SPARQL provides ordering of results by using ORDER BY, see here

The ordering in your example is based on the number of references of a statement. Here is a non-optimized version that does what you want:

SELECT ?actor ?actorLabel WHERE {
  ?movie p:P161 ?statement .
  ?statement ps:P161 ?actor .
  OPTIONAL {?statement prov:wasDerivedFrom ?ref . }
  SERVICE wikibase:label { bd:serviceParam wikibase:language "en". }
  FILTER(?movie = wd:Q102754)
}
group by ?movie ?actor ?actorLabel
ORDER BY DESC(count(?ref)) ASC(?actorLabel)
LIMIT 1000


来源:https://stackoverflow.com/questions/42387768/recover-the-original-order

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!