query-ae814188efaae629cc6faf13c8cc13e7

rq turtle/ttl

Row numberingHello, is there a way to compute the line number of the sorted rows (aka fake rank) ? Eg in that query where it'd be nice to have 1,2....,19,20.

Use at

PREFIX wikibase: <http://wikiba.se/ontology#>
PREFIX wdt: <http://www.wikidata.org/prop/direct/>
PREFIX wd: <http://www.wikidata.org/entity/>
PREFIX ps: <http://www.wikidata.org/prop/statement/>
PREFIX prov: <http://www.w3.org/ns/prov#>
PREFIX pr: <http://www.wikidata.org/prop/reference/>
PREFIX pqv: <http://www.wikidata.org/prop/qualifier/value/>
PREFIX pq: <http://www.wikidata.org/prop/qualifier/>
PREFIX p: <http://www.wikidata.org/prop/>
PREFIX bd: <http://www.bigdata.com/rdf#>
SELECT ?item ?itemLabel (MAX(?number) AS ?passengers)
WHERE
{
  {
    # Get the sum of monthly values within a year
    SELECT ?item ?year (SUM(?max_numberperperiod) AS ?number) (SAMPLE(?monthly_reference_URL) AS ?monthly_reference_URL2)
    WHERE
    {
      # Get the maximal value and a sample reference URL for each unique month
      {
        SELECT ?item ?year (MAX(?numberperperiod) AS ?max_numberperperiod) (SAMPLE(?reference_URL) AS ?monthly_reference_URL)
        WHERE
        {

{
  SELECT ?item ?statement ?date ?year ?timevalue ?numberperperiod ?reference_URL
  WHERE
  {
    ?item wdt:P238 ?airport_code.
    ?item p:P3872 ?statement.
    ?statement pqv:P585 ?timevalue;
               ps:P3872 ?numberperperiod.
    ?timevalue wikibase:timeValue ?date.
    OPTIONAL { ?statement pq:P518 ?applies. }
    OPTIONAL { ?statement prov:wasDerivedFrom / (pr:P854|pr:P4656) ?reference_URL. }
    FILTER (BOUND(?applies)=false || ?applies = wd:Q2165236 )
    MINUS { ?statement wikibase:rank wikibase:DeprecatedRank }
    BIND (YEAR(?date) AS ?year)
    FILTER (?year =2020).
  }
}          ?timevalue wikibase:timePrecision ?prec.
          FILTER (?prec > 9)# precision more precise or equal to month
        }
        GROUP BY ?item ?year ?date
      }
    }
    GROUP BY ?item ?year
  }
  UNION
  {
    ?timevalue wikibase:timePrecision 9 .
    BIND (?numberperperiod AS ?number)
    BIND (?reference_URL AS ?sample_reference_URL)

{
  SELECT ?item ?statement ?date ?year ?timevalue ?numberperperiod ?reference_URL
  WHERE
  {
    ?item wdt:P238 ?airport_code.
    ?item p:P3872 ?statement.
    ?statement pqv:P585 ?timevalue;
               ps:P3872 ?numberperperiod.
    ?timevalue wikibase:timeValue ?date.
    OPTIONAL { ?statement pq:P518 ?applies. }
    OPTIONAL { ?statement prov:wasDerivedFrom / (pr:P854|pr:P4656) ?reference_URL. }
    FILTER (BOUND(?applies)=false || ?applies = wd:Q2165236 )
    MINUS { ?statement wikibase:rank wikibase:DeprecatedRank }
    BIND (YEAR(?date) AS ?year)
    FILTER (?year =2020).
  }
}  }
  SERVICE wikibase:label { bd:serviceParam wikibase:language "en,fr". }
}
GROUP BY ?item ?itemLabel ?year
ORDER BY DESC (?passengers)
limit 20

Query found at

graph TD classDef projected fill:lightgreen; classDef literal fill:orange; classDef iri fill:yellow; v6("?airport_code") v4("?applies") v10("?date") v5("?item"):::projected v12("?max_numberperperiod") v13("?monthly_reference_URL") v15("?monthly_reference_URL2") v16("?number"):::projected v9("?numberperperiod") v17("?passengers") v2("?prec") v11("?reference_URL") v16("?sample_reference_URL") v7("?statement") v8("?timevalue") v17("?year") a1((" ")) a2((" ")) c15(["wikibase:DeprecatedRank"]):::iri c20(["en,fr"]):::literal c18(["bd:serviceParam"]):::iri c1(["9^^xsd:integer"]):::literal subgraph union0[" Union "] subgraph union0l[" "] style union0l fill:#abf,stroke-dasharray: 3 3; v8 --"wikibase:timePrecision"--> c1 bind0[/"?numberperperiod"/] v9 --o bind0 bind0 --as--o v16 bind1[/"?reference_URL"/] v11 --o bind1 bind1 --as--o v16 f2[["?year = '2020^^xsd:integer'"]] f2 --> v17 f3[["(bound(?applies) = 'false^^xsd:boolean' || ?applies = 'wd:Q2165236')"]] f3 --> v4 v5 --"p:direct/P238"--> v6 v5 --"p:P3872"--> v7 v7 --"p:qualifier/value/P585"--> v8 v7 --"p:statement/P3872"--> v9 v8 --"wikibase:timeValue"--> v10 subgraph optional0["(optional)"] style optional0 fill:#bbf,stroke-dasharray: 5 5; v7 -."p:qualifier/P518".-> v4 end subgraph optional1["(optional)"] style optional1 fill:#bbf,stroke-dasharray: 5 5; v7 -."prov:wasDerivedFrom".-> a2 subgraph union1[" Union "] subgraph union1l[" "] style union1l fill:#abf,stroke-dasharray: 3 3; a2 --"p:reference/P4656"--> v11 end subgraph union1r[" "] style union1r fill:#abf,stroke-dasharray: 3 3; a2 --"p:reference/P854"--> v11 end union1r <== or ==> union1l end end subgraph minus4["MINUS"] style minus4 stroke-width:6px,fill:pink,stroke:red; v7 --"wikibase:rank"--> c15 end bind5[/"year-from-dateTime(?date)"/] v10 --o bind5 bind5 --as--o v17 end subgraph union0r[" "] style union0r fill:#abf,stroke-dasharray: 3 3; f6[["?prec > '9^^xsd:integer'"]] f6 --> v2 f7[["?year = '2020^^xsd:integer'"]] f7 --> v17 f8[["(bound(?applies) = 'false^^xsd:boolean' || ?applies = 'wd:Q2165236')"]] f8 --> v4 v5 --"p:direct/P238"--> v6 v5 --"p:P3872"--> v7 v7 --"p:qualifier/value/P585"--> v8 v7 --"p:statement/P3872"--> v9 v8 --"wikibase:timeValue"--> v10 subgraph optional2["(optional)"] style optional2 fill:#bbf,stroke-dasharray: 5 5; v7 -."p:qualifier/P518".-> v4 end subgraph optional3["(optional)"] style optional3 fill:#bbf,stroke-dasharray: 5 5; v7 -."prov:wasDerivedFrom".-> a1 subgraph union2[" Union "] subgraph union2l[" "] style union2l fill:#abf,stroke-dasharray: 3 3; a1 --"p:reference/P4656"--> v11 end subgraph union2r[" "] style union2r fill:#abf,stroke-dasharray: 3 3; a1 --"p:reference/P854"--> v11 end union2r <== or ==> union2l end end subgraph minus9["MINUS"] style minus9 stroke-width:6px,fill:pink,stroke:red; v7 --"wikibase:rank"--> c15 end bind10[/"year-from-dateTime(?date)"/] v10 --o bind10 bind10 --as--o v17 v8 --"wikibase:timePrecision"--> v2 bind13[/"max(?numberperperiod)"/] v9 --o bind13 bind13 --as--o v12 bind14[/"sample(?reference_URL)"/] v11 --o bind14 bind14 --as--o v13 bind17[/"sum(?max_numberperperiod)"/] v12 --o bind17 bind17 --as--o v16 bind18[/"sample(?monthly_reference_URL)"/] v13 --o bind18 bind18 --as--o v15 end union0r <== or ==> union0l end subgraph s1["http://wikiba.se/ontology#label"] style s1 stroke-width:4px; c18 --"wikibase:language"--> c20 end bind20[/"max(?number)"/] v16 --o bind20 bind20 --as--o v17