sparql-examples

A set of SPARQL examples that are used in different TGX resources

View the Project on GitHub BiGCAT-UM/sparql-examples

012

rq turtle/ttl

Author aspect: pages per year

Use at

PREFIX p: <http://www.wikidata.org/prop/>
PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#>
PREFIX wdt: <http://www.wikidata.org/prop/direct/>
PREFIX wd: <http://wikidata.org/entity/>
#defaultView:BarChart
PREFIX target: <http://www.wikidata.org/entity/Q97270>

select ?year ?number_of_pages ?work_label where {
  {
    select ?year (sample(?pages) as ?number_of_pages) ?work_label where {
      {
        select (str(?year_) as ?year) (0 as ?pages) ("_" as ?work_label) where {
          ?year_item wdt:P31 wd:Q577 . 
          ?year_item wdt:P585 ?date .
          bind(year(?date) as ?year_)
          {
            select (min(?year_) as ?earliest_year) (max(?year_) as ?latest_year) where {
              ?work wdt:P50 target: .
              ?work wdt:P577 ?publication_date . 
              bind(year(?publication_date) as ?year_)
            }
          }
          filter (?year_ >= ?earliest_year && ?year_ <= ?latest_year)
        }
      }
      union {
        ?work wdt:P50 target: .
        ?work wdt:P1104 ?pages .
        ?work wdt:P577 ?date . 
        ?work rdfs:label ?long_work_label . filter(lang(?long_work_label) = 'en')
        bind(substr(?long_work_label, 1, 20) as ?work_label)
        bind(str(year(?date)) as ?year) 
      }
	} 
	group by ?year ?work ?work_label
	order by ?year 
  }
}

graph TD
classDef projected fill:lightgreen;
classDef literal fill:orange;
classDef iri fill:yellow;
  v6("?date")
  v9("?earliest_year")
  v9("?latest_year")
  v11("?long_work_label")
  v12("?number_of_pages"):::projected 
  v9("?pages")
  v8("?publication_date")
  v7("?work")
  v12("?work_label"):::projected 
  v12("?year"):::projected 
  v9("?year_")
  v5("?year_item")
  c2([http://wikidata.org/entity/Q577]):::iri 
  c5(["wd:Q97270"]):::iri 
  subgraph union0[" Union "]
  subgraph union0l[" "]
    style union0l fill:#abf,stroke-dasharray: 3 3;
    f0[["?long_work_label = 'en'"]]
    f0 --> v11
    v7 --"p:direct/P50"-->  c5
    v7 --"p:direct/P1104"-->  v9
    v7 --"p:direct/P577"-->  v6
    v7 --"rdfs:label"-->  v11
    bind1[/"substring(?long_work_label,'1^^xsd:integer','20^^xsd:integer')"/]
    v11 --o bind1
    bind1 --as--o v12
    bind2[/"str(year-from-dateTime(?date))"/]
    v6 --o bind2
    bind2 --as--o v12
  end
  subgraph union0r[" "]
    style union0r fill:#abf,stroke-dasharray: 3 3;
    f3[["?year_ >= ?earliest_year?year_ <= ?latest_year"]]
    f3 --> v9
    f3 --> v9
    f3 --> v9
    v5 --"p:direct/P31"-->  c2
    v5 --"p:direct/P585"-->  v6
    bind4[/"year-from-dateTime(?date)"/]
    v6 --o bind4
    bind4 --as--o v9
    v7 --"p:direct/P50"-->  c5
    v7 --"p:direct/P577"-->  v8
    bind5[/"year-from-dateTime(?publication_date)"/]
    v8 --o bind5
    bind5 --as--o v9
    bind8[/"min(?year_)"/]
    v9 --o bind8
    bind8 --as--o v9
    bind9[/"max(?year_)"/]
    v9 --o bind9
    bind9 --as--o v9
    bind10[/"str(?year_)"/]
    v9 --o bind10
    bind10 --as--o v12
    bind11[/"'0^^xsd:integer'"/]
    bind11 --as--o v9
    bind12[/"'_'"/]
    bind12 --as--o v12
  end
  union0r <== or ==> union0l
  end
  bind14[/"sample(?pages)"/]
  v9 --o bind14
  bind14 --as--o v12