query-86c4aec297f3cd93a21b01b08758d8e2

rq turtle/ttl

Get median year for discovery of elements SELECT ?item ?itemLabel ?year ?lower_or_equal_values ?higher_or_equal_values WITH { SELECT ?item ?year WHERE { ?item wdt:P31 wd:Q11344 . ?item wdt:P575 ?time_of_discovery . BIND (YEAR(?time_of_discovery) AS ?year)
} } AS %data_set WITH { SELECT ?item ?year (COUNT(?year2) AS ?lower_or_equal_values) WHERE { INCLUDE %data_set { SELECT (?year AS ?year2) WHERE { INCLUDE %data_set } } FILTER (?year2 <= ?year) } GROUP BY ?item ?year } AS %lower_or_equal_values WITH { SELECT ?item ?year (COUNT(?year2) AS ?higher_or_equal_values) WHERE { INCLUDE %data_set { SELECT (?year AS ?year2) WHERE { INCLUDE %data_set } } FILTER (?year2 >= ?year) } GROUP BY ?item ?year } AS %higher_or_equal_values WHERE { INCLUDE %lower_or_equal_values INCLUDE %higher_or_equal_values FILTER (?higher_or_equal_values <= ?lower_or_equal_values) SERVICE wikibase:label { bd:serviceParam wikibase:language "[AUTO_LANGUAGE],en" } } ORDER BY ?lower_or_equal_values LIMIT 1

Use at

PREFIX wikibase: <http://wikiba.se/ontology#>
PREFIX wdt: <http://www.wikidata.org/prop/direct/>
PREFIX wd: <http://www.wikidata.org/entity/>
PREFIX bd: <http://www.bigdata.com/rdf#>
# Get median year for discovery of elements
SELECT ?item ?itemLabel ?year ?lower_or_equal_values ?higher_or_equal_values
WHERE
{

{
  SELECT ?item ?year (COUNT(?year2) AS ?lower_or_equal_values)
  WHERE
  {

{
  SELECT ?item ?year
  WHERE
  {
    ?item wdt:P31 wd:Q11344 .
    ?item wdt:P575 ?time_of_discovery .
    BIND (YEAR(?time_of_discovery) AS ?year)        
  }
}    { SELECT (?year AS ?year2) WHERE { 
{
  SELECT ?item ?year
  WHERE
  {
    ?item wdt:P31 wd:Q11344 .
    ?item wdt:P575 ?time_of_discovery .
    BIND (YEAR(?time_of_discovery) AS ?year)        
  }
}} }
    FILTER (?year2 <= ?year)
  }
  GROUP BY ?item ?year
}  
{
  SELECT ?item ?year (COUNT(?year2) AS ?higher_or_equal_values)
  WHERE
  {

{
  SELECT ?item ?year
  WHERE
  {
    ?item wdt:P31 wd:Q11344 .
    ?item wdt:P575 ?time_of_discovery .
    BIND (YEAR(?time_of_discovery) AS ?year)        
  }
}    { SELECT (?year AS ?year2) WHERE { 
{
  SELECT ?item ?year
  WHERE
  {
    ?item wdt:P31 wd:Q11344 .
    ?item wdt:P575 ?time_of_discovery .
    BIND (YEAR(?time_of_discovery) AS ?year)        
  }
}} }
    FILTER (?year2 >= ?year)
  }
  GROUP BY ?item ?year
}  FILTER (?higher_or_equal_values <= ?lower_or_equal_values)
  SERVICE wikibase:label { bd:serviceParam wikibase:language "[AUTO_LANGUAGE],en" }
}
ORDER BY ?lower_or_equal_values
LIMIT 1

Query found at

graph TD classDef projected fill:lightgreen; classDef literal fill:orange; classDef iri fill:yellow; v7("?higher_or_equal_values"):::projected v5("?item"):::projected v7("?lower_or_equal_values"):::projected v6("?time_of_discovery") v7("?year"):::projected v7("?year2") c5(["bd:serviceParam"]):::iri c7(["#91;AUTO_LANGUAGE#93;,en"]):::literal c2(["wd:Q11344"]):::iri f0[["?higher_or_equal_values <= ?lower_or_equal_values"]] f0 --> v7 f0 --> v7 f1[["?year2 <= ?year"]] f1 --> v7 f1 --> v7 v5 --"wdt:P31"--> c2 v5 --"wdt:P575"--> v6 bind2[/"year-from-dateTime(?time_of_discovery)"/] v6 --o bind2 bind2 --as--o v7 v5 --"wdt:P31"--> c2 v5 --"wdt:P575"--> v6 bind3[/"year-from-dateTime(?time_of_discovery)"/] v6 --o bind3 bind3 --as--o v7 bind4[/"?year"/] v7 --o bind4 bind4 --as--o v7 bind6[/"count(?year2)"/] v7 --o bind6 bind6 --as--o v7 f7[["?year2 >= ?year"]] f7 --> v7 f7 --> v7 v5 --"wdt:P31"--> c2 v5 --"wdt:P575"--> v6 bind8[/"year-from-dateTime(?time_of_discovery)"/] v6 --o bind8 bind8 --as--o v7 v5 --"wdt:P31"--> c2 v5 --"wdt:P575"--> v6 bind9[/"year-from-dateTime(?time_of_discovery)"/] v6 --o bind9 bind9 --as--o v7 bind10[/"?year"/] v7 --o bind10 bind10 --as--o v7 bind12[/"count(?year2)"/] v7 --o bind12 bind12 --as--o v7 subgraph s1["http://wikiba.se/ontology#label"] style s1 stroke-width:4px; c5 --"wikibase:language"--> c7 end