Search code examples
sparqlwikidata

How do I create a Wikidata SPARQL query to return the top 10 US companies and their directors based on revenue?


Here is the query I am working with:

SELECT DISTINCT ?item ?itemLabel ?rev ?date (group_concat(distinct ?directorLabel ; separator = ", ") as ?directors)
WHERE { 
  ?item wdt:P31/wdt:P279* wd:Q4830453 ;
        wdt:P17 wd:Q30 .
  ?item p:P2139 ?revSt .
  ?revSt pq:P585 ?date.
  FILTER (   YEAR(?date) = 2020 ).
  ?revSt ps:P2139 ?rev .
  ?item p:P3320 ?relationship.
  ?relationship ps:P3320 ?director.
  #Filter Director by no end date
  FILTER NOT EXISTS {?relationship pq:P582 ?end.}
  #Group directors
  SERVICE wikibase:label { 
    bd:serviceParam wikibase:language "[AUTO_LANGUAGE],en". 
    ?director rdfs:label ?directorLabel .
    ?item rdfs:label ?itemLabel .
  }
 }
GROUP BY ?item ?itemLabel
ORDER BY DESC(?rev) 
LIMIT 10

This query is a bad aggregate, if I add more variables to the GROUP BY clause it times out. Ultimately I would like to aggregate the wdt ids for all the directors too.


Solution

  • SELECT DISTINCT 
    ?item ?itemLabel ?rev ?date 
    (group_concat(distinct ?directorLabel ; separator = ", ") as ?directors) 
    WHERE {    
      ?item wdt:P31/wdt:P279* wd:Q4830453 ;
            wdt:P17 wd:Q30 .
      ?item p:P2139 ?revSt .   
      ?revSt pq:P585 ?date. 
      hint:Prior hint:rangeSafe "true" .
      FILTER (   YEAR(?date) = 2020 ).   
      ?revSt ps:P2139 ?rev .  
      ?item p:P3320 ?relationship.   
      ?relationship ps:P3320 ?director.   
      FILTER NOT EXISTS {?relationship pq:P582 ?end.}  
      SERVICE wikibase:label { 
        bd:serviceParam wikibase:language "[AUTO_LANGUAGE],en".   
        ?director rdfs:label ?directorLabel .    
        ?item rdfs:label ?itemLabel .  
       } 
     } 
    GROUP BY ?item ?itemLabel ?rev ?date 
    ORDER BY DESC(?rev)  
    LIMIT 10