'Get multiple optional properties with multiple optional qualifiers on Wikidata Query Service

I want to load the following data of specific humans with the Wikidata Query Service:

properties

  • birth date
  • lifestyles

qualifiers

  • start times of the lifestyles
  • end times of the lifestyles

And I want exactly one row for each human in the output, no matter if no properties/qualifiers or multiple properties/qualifiers are set.

I managed to create a query which works with humans who have at least one lifestyle, but when there's none, the query takes a long time and instead of empty cells many lifestyles, start and end times are loaded which are not related to the human.

This is the query for the humans Q185140, Q463434 and Q3378937 and Q3378937 doesn't have a lifestyle, but still the lifestyles, lifestylesStarts and lifestylesEnds cells aren't empty as expected and instead full of values:

SELECT ?human ?humanLabel 
(GROUP_CONCAT(DISTINCT ?lifestyleLabel;separator="|") AS ?lifestyles) 
(GROUP_CONCAT(DISTINCT ?lifestyleStart;separator="|") AS ?lifestylesStarts) 
(GROUP_CONCAT(DISTINCT ?lifestyleEnd;separator="|") AS ?lifestylesEnds) 
(GROUP_CONCAT(DISTINCT ?dateBirth;separator="|") AS ?dateBirths) 
WHERE { 
    VALUES ?human { wd:Q185140 wd:Q463434 wd:Q3378937 } . 
    OPTIONAL{ ?human p:P1576 ?statement . }
    OPTIONAL{ ?statement ps:P1576 ?lifestyle . } 
    OPTIONAL{ ?statement pq:P580 ?lifestyleStart . } 
    OPTIONAL{ ?statement pq:P582 ?lifestyleEnd . } 
    OPTIONAL{ ?human wdt:P569 ?dateBirth . } 
    SERVICE wikibase:label { 
        bd:serviceParam wikibase:language "[AUTO_LANGUAGE],en" . 
        ?human rdfs:label ?humanLabel . 
        ?lifestyle rdfs:label ?lifestyleLabel . 
    }
}
GROUP BY ?human ?humanLabel

You can run the query and see the output here.

What do I have to change to get empty cells if properties/qualifiers are not set?



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source