Search code examples
sparqlsemantic-web

SPARQL : I have individual with multiple values for single object property. How to convert to one record?


When I query it shows three records for the same individual with three different values for the same object property. So how can I convert to a single record? screenshot of query and result screenshot of query and result


Solution

  • Indeed GROUP_CONCAT is your friend. Please find an example below.

    Nick names of US Presidents

    SPARQL : I have individual with multiple values for single object property. How to convert to one record?

    query

    SELECT ?item ?itemLabel (GROUP_CONCAT(DISTINCT ?nickName; SEPARATOR=",") as ?nickNames)
    WHERE 
    {
      # president
      ?item wdt:P39 wd:Q11696.
      ?item wdt:P1449 ?nickName
      SERVICE wikibase:label { bd:serviceParam wikibase:language "en". }
    } GROUP BY ?item ?itemLabel
    

    try it!

    result

    item itemLabel nickNames
    http://www.wikidata.org/entity/Q23 George Washington American Fabius
    http://www.wikidata.org/entity/Q76 Barack Obama Barry
    http://www.wikidata.org/entity/Q1124 Bill Clinton The Comeback Kid,Slick Willie
    http://www.wikidata.org/entity/Q6279 Joe Biden Joe
    http://www.wikidata.org/entity/Q9588 Richard Nixon Dick,Dick Nixon
    http://www.wikidata.org/entity/Q9696 John F. Kennedy Jack
    http://www.wikidata.org/entity/Q9916 Dwight D. Eisenhower Ike
    http://www.wikidata.org/entity/Q11817 Andrew Jackson Old Hickory
    http://www.wikidata.org/entity/Q11820 Martin Van Buren Old Kinderhook
    http://www.wikidata.org/entity/Q11869 William Henry Harrison Old Tippecanoe
    http://www.wikidata.org/entity/Q11896 Zachary Taylor Old Rough and Ready
    http://www.wikidata.org/entity/Q22686 Donald Trump The Donald
    http://www.wikidata.org/entity/Q34836 Ulysses S Grant “Unconditional Surrender” Grant