3

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?

logi-kal
  • 7,107
  • 6
  • 31
  • 43
Adrian
  • 41
  • 2
  • 1
    https://w.wiki/HDp? 1. use nested `optional`s 2. don't start with `optional`. 3. do you really need `group_concat`? – Stanislav Kralin Feb 14 '20 at 15:16
  • 1
    @StanislavKralin regarding point 3 -> *"And I want exactly one row for each human in the output, no matter if no properties/qualifiers or multiple properties/qualifiers are set."* - so if a human has multiple lifestyles, I assume yes. BUT: the main issue is, that with multiple lifestyles everything would be mixed up, i.e. you wouldn't have the relation between which lifestyle started and ended when, because as you know - order doesn't work in `group_concat` - so in the current example, it's impossible to know for "Marianne Thieme" the relation between "vegetarianism|veganism" and the dates – UninformedUser Feb 14 '20 at 15:48
  • here we go (https://w.wiki/HE7) :`SELECT ?human ?humanLabel (GROUP_CONCAT(DISTINCT ?lifestyle;separator="|") AS ?lifestyles) (GROUP_CONCAT(DISTINCT ?dateBirth;separator="|") AS ?dateBirths) WHERE { VALUES ?human { wd:Q185140 wd:Q463434 wd:Q3378937 } . ?human wdt:P31 wd:Q5 . ` – UninformedUser Feb 14 '20 at 16:01
  • `OPTIONAL{ ?human p:P1576 ?statement . ?statement ps:P1576/rdfs:label ?lifestyleLabel filter (lang(?lifestyleLabel) = 'en') OPTIONAL{ ?statement pq:P580 ?lifestyleStart } OPTIONAL{ ?statement pq:P582 ?lifestyleEnd } BIND(IF(BOUND(?lifestyleEnd), CONCAT(" -- ", str(?lifestyleEnd)), "") as ?lsEndText) BIND(CONCAT(str(?lifestyleLabel), " (", str(?lifestyleStart), ?lsEndText, ")") as ?lifestyle) }OPTIONAL{ ?human wdt:P569 ?dateBirth . } SERVICE wikibase:label { bd:serviceParam wikibase:language "[AUTO_LANGUAGE],en" . ?human rdfs:label ?humanLabel . }}GROUP BY ?human ?humanLabel` – UninformedUser Feb 14 '20 at 16:02
  • @StanislavKralin Thanks for helping me out with the misplaced curly bracket. Regarding 2.: Why shouldn't I start with an `optional`? The thing is, in the end I also want to list non-human instances so every value can be variable. – Adrian Feb 21 '20 at 13:18
  • 1
    @UninformedUser You're right regarding Stanislav's 3. point. Your code fixes the issue you mentioned like a charm, thanks a lot for this! – Adrian Feb 21 '20 at 13:22
  • As for `optional`, see Q1b in https://blog.blazegraph.com/?p=928 – Stanislav Kralin Feb 21 '20 at 18:26

0 Answers0