Search code examples
sparqlwikidata

WIkidata: Getting multiple participants in Wikidata query result


Question: Hi, all. I found an example script from Wikidata example pages, but in the result, every case shows only ONE participant, while most of them have many participants. Is there a way to show all the participants labels? I don't understand why (and how) only one is selected to appear.

SELECT distinct ?race ?raceLabel ?musherLabel
WHERE
{
{ ?race wdt:P31/wdt:P279* wd:Q1968664 . }
UNION { ?race wdt:P31/wdt:P641* wd:Q1968664 . }
?race p:P710 ?musherS .     #here we have a full statement, not a value
?musherS ps:P710 ?musher .  #here we get the value
FILTER NOT EXISTS { ?musherS pq:P1352 ?rank }
FILTER NOT EXISTS { ?musherS pq:P793 ?event }
SERVICE wikibase:label { bd:serviceParam wikibase:language "fr,en" }
}
ORDER BY ?raceLabel

Result

Edit:

Here (https://www.wikidata.org/wiki/Help:Qualifiers) I read:

For multiple values

Example of a statement consisting of multiple values for one property Wikidata allows for items to have multiple values per property. For certain statements—such as the children of a person or the official languages of a country—it is perfectly reasonable to have multiple values which may not be qualified in any way.

However, it is also possible for items to have statements with multiple values when only really one value is expected for the property. When an item ideally should have only one value—for example, the population of a city—but has multiple values, then qualifiers should be used in order to indicate further information about the values, such as when the values date from, how they were determined, what exactly they refer to, and so on.

So does it mean there is NO way to just get them all (label values)?


Solution

  • For a single row per race you have to make use of group by in combination with the aggregate function group_concat.

    Query:

    SELECT distinct ?race ?raceLabel (group_concat(?musherLabel; separator="; ") as ?mushers)
    WHERE
    {
        { ?race wdt:P31/wdt:P279* wd:Q1968664 . }
        UNION 
        { ?race wdt:P31/wdt:P641* wd:Q1968664 . }
        ?race p:P710 ?musherS .     #here we have a full statement, not a value
        ?musherS ps:P710 ?musher .  #here we get the value
        FILTER NOT EXISTS { ?musherS pq:P1352 ?rank }
        FILTER NOT EXISTS { ?musherS pq:P793 ?event }
    
        SERVICE wikibase:label { 
         bd:serviceParam wikibase:language "en". 
         ?musher rdfs:label ?musherLabel . 
         ?race rdfs:label ?raceLabel . 
    
      }
    }
    GROUP BY ?race ?raceLabel
    ORDER BY ?raceLabel
    

    Output:

    +------------------------------------------+--------------------+-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
    |                   race                   |     raceLabel      |                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                     mushers                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                     |
    +------------------------------------------+--------------------+-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
    | http://www.wikidata.org/entity/Q19361352 | Q19361352          | Brett Bruggeman; Brett Bruggeman; Brett Bruggeman; Brett Bruggeman; Bryce Mumford; Bryce Mumford; Bryce Mumford; Bryce Mumford                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                  |
    | http://www.wikidata.org/entity/Q18577453 | 1993 Iditarod      | Beverly Masek                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                   |
    | http://www.wikidata.org/entity/Q18577456 | 1996 Iditarod      | Rick Swenson                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                    |
    | http://www.wikidata.org/entity/Q23014078 | 2016 Iditarod      | Robert Redington; Hans Gatt; Anna Berington; DeeDee Jonrowe; Noah Burmeister; Ed Stielstra; Ken Anderson; Tim Pappas; Ellen Halverson; Jodi Bailey; Jeff King; Brent Sass; Becca Moore; Robert Bundtzen; Karin Hendrickson; Sigrid Ekran; Melissa Owens; Rob Cooke; Scott Smith; Kim Franklin; Elliot Anderson; Scott Janssen; Paige Drobny; Ketil Reitan; Justin Savidis; Aliy Zirkle; Cim Smyth; Dag Torulf Olsen; Linwood Fiedler; Kristin Bacon; Monica Zappa; Billy Snodgrass; Tore Albrigtsen; Mats Pettersson; Kristin Knight Pace; Cindy Gallea; Ray Redington; Katherine Keith; James Volek; Joar Leifseth Ulsom; Jim Lanier; Martin Koenig; Ryne Olson; Nathan Schroeder; Hugh Neff; Tom Jamgochian; Larry Daugherty; Lisbet Norris; Zoya DeNure; Ralph Johannesen; Allen Moore; Michael Williams; Sarah Stokey; Rick Casillo; John Baker; Paul Gebhardt; Dallas Seavey; Mary Helwig; Michelle Phillips; Martin Buser; Kristy Berington; Jan Steves; Ryan Redington; Trent Herbst; Jason Campeau; Alan Eischens; Mitch Seavey; Wade Marrs; Richie Diehl; Nicolas Petit; Charley Bejna; Patrick Beall; Lance Mackey; Robert Sørlie; Jessie Royer; Miriam Osredkar; Kelly Maixner; Noah Pereira; Travis Beals; Geir Idar Hjelvik; Matthew Failor; Peter Kaiser; Lars Monsen; Jason Mackey; Cody Strathe |
    | http://www.wikidata.org/entity/Q22093610 | 2016 Kuskokwim 300 | Pato Geron                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                      |
    +------------------------------------------+--------------------+-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+