query-8c0881f7d2f562cbef0e97dcc36358ed

rq turtle/ttl

Average birth-year by occupation in Lur Hiztegi Entziklopedikoa

Select the desired columns and get labels

SELECT ?occ ?occLabel ?avgBirthYear ?count WHERE { { # Group the people by their occupation and calculate age SELECT ?occ (count(?p) as ?count) (round(avg(?birthYear)) as ?avgBirthYear) WHERE { { # Get people with occupation + birth/death dates; combine multiple birth/death dates using avg SELECT ?p ?occ (avg(year(?birth)) as ?birthYear) WHERE { ?p wdt:P31 wd:Q5 ; wdt:P10242 ?lur ; wdt:P106 ?occ ; p:P569/psv:P569 [ wikibase:timePrecision "9"^^xsd:integer ; # precision of at least year wikibase:timeValue ?birth ; ] . } GROUP BY ?p ?occ } } GROUP BY ?occ }

FILTER (?count > 30) # arbitrary number to weed out values with 'few' observations SERVICE wikibase:label { bd:serviceParam wikibase:language "eu,en" . } } ORDER BY ASC(?avgBirthYear)

Use at

PREFIX xsd: <http://www.w3.org/2001/XMLSchema#>
PREFIX wikibase: <http://wikiba.se/ontology#>
PREFIX wdt: <http://www.wikidata.org/prop/direct/>
PREFIX wd: <http://www.wikidata.org/entity/>
PREFIX psv: <http://www.wikidata.org/prop/statement/value/>
PREFIX p: <http://www.wikidata.org/prop/>
PREFIX bd: <http://www.bigdata.com/rdf#>
#Average birth-year by occupation in Lur Hiztegi Entziklopedikoa
# Select the desired columns and get labels
SELECT ?occ ?occLabel ?avgBirthYear ?count
WHERE
{
  {
    # Group the people by their occupation and calculate age
    SELECT
      ?occ
        (count(?p) as ?count)
        (round(avg(?birthYear)) as ?avgBirthYear)
    WHERE {
      {
        # Get people with occupation + birth/death dates; combine multiple birth/death dates using avg
        SELECT
          ?p
            ?occ
            (avg(year(?birth)) as ?birthYear)
        WHERE {
           ?p  wdt:P31 wd:Q5 ;
               wdt:P10242 ?lur ;
              wdt:P106 ?occ ;
              p:P569/psv:P569 [
                wikibase:timePrecision "9"^^xsd:integer ; # precision of at least year
                wikibase:timeValue ?birth ;
              ] .
        }
        GROUP BY ?p ?occ
      }
    }
    GROUP BY ?occ
  }

  FILTER (?count > 30) # arbitrary number to weed out values with 'few' observations
  SERVICE wikibase:label { bd:serviceParam wikibase:language "eu,en" . }
}
ORDER BY ASC(?avgBirthYear)

Query found at

graph TD classDef projected fill:lightgreen; classDef literal fill:orange; classDef iri fill:yellow; v8("?_anon_42b5416610844a34809f33a099ae9de1153210") v9("?avgBirthYear"):::projected v6("?birth") v7("?birthYear") v9("?count"):::projected v4("?lur") v5("?occ"):::projected v3("?p") a2((" ")) a1((" ")) c14(["eu,en"]):::literal c12(["bd:serviceParam"]):::iri c3(["wd:Q5"]):::iri c7(["9^^xsd:integer"]):::literal f0[["?count > '30^^xsd:integer'"]] f0 --> v9 v3 --"p:direct/P31"--> c3 v3 --"p:direct/P10242"--> v4 v3 --"p:direct/P106"--> v5 a1 --"wikibase:timePrecision"--> c7 a1 --"wikibase:timeValue"--> v6 v3 --"p:P569"--> a2 a2 --"p:statement/value/P569"--> a1 bind2[/"average(year-from-dateTime(?birth))"/] v6 --o bind2 bind2 --as--o v7 bind5[/"average(?birthYear)"/] v7 --o bind5 bind5 --as--o v8 bind6[/"count(?p)"/] v3 --o bind6 bind6 --as--o v9 bind7[/"numeric-round()"/] null --o bind7 bind7 --as--o v9 subgraph s1["http://wikiba.se/ontology#label"] style s1 stroke-width:4px; c12 --"wikibase:language"--> c14 end