query-3a2aff58b6861094c9247e909e4ab28f

rq turtle/ttl

Stars up to 18 light years away sorted by the nearest SELECT ?star ?starLabel ?distance ?unit ?unitLabel ?lightyears WHERE { { SELECT ?star ?distance ?unit ?lightyears WHERE { ?star wdt:P31 wd:Q523. ?star p:P2583/psv:P2583 ?d. ?d wikibase:quantityUnit ?unit. ?d wikibase:quantityAmount ?distance. BIND( COALESCE( IF(?unit = wd:Q531, ?distance, 1/0), # lightyear IF(?unit = wd:Q12129, ?distance3.2616, 1/0), # paresc IF(?unit = wd:Q11929860, ?distance3261.6, 1/0), # kiloparsec IF(?unit = wd:Q3773454, ?distance*3261600, 1/0), # megaparsec -1 ) AS ?lightyears) FILTER( ?lightyears < 19) } LIMIT 100 } SERVICE wikibase:label { bd:serviceParam wikibase:language "[AUTO_LANGUAGE],en" } } ORDER BY ?lightyears LIMIT 100

Use at

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#>
# Stars up to 18 light years away sorted by the nearest
SELECT ?star ?starLabel ?distance ?unit ?unitLabel ?lightyears
WHERE
{
  {
    SELECT ?star ?distance ?unit ?lightyears
    WHERE
    {
      ?star wdt:P31 wd:Q523.
      ?star p:P2583/psv:P2583 ?d.
      ?d wikibase:quantityUnit ?unit.
      ?d wikibase:quantityAmount ?distance.
      BIND(
        COALESCE(
          IF(?unit = wd:Q531, ?distance, 1/0), # lightyear
          IF(?unit = wd:Q12129, ?distance*3.2616, 1/0), # paresc
          IF(?unit = wd:Q11929860, ?distance*3261.6, 1/0), # kiloparsec
          IF(?unit = wd:Q3773454, ?distance*3261600, 1/0), # megaparsec
          -1
      ) AS ?lightyears)
      FILTER( ?lightyears < 19)
    }
    LIMIT 100
  }
  SERVICE wikibase:label { bd:serviceParam wikibase:language "[AUTO_LANGUAGE],en" }
}
ORDER BY ?lightyears
LIMIT 100

Query found at

graph TD classDef projected fill:lightgreen; classDef literal fill:orange; classDef iri fill:yellow; v3("?d") v5("?distance"):::projected v6("?lightyears"):::projected v2("?star"):::projected v4("?unit"):::projected a1((" ")) c3(["wd:Q523"]):::iri c9(["bd:serviceParam"]):::iri c11(["#91;AUTO_LANGUAGE#93;,en"]):::literal f0[["?lightyears < '19^^xsd:integer'"]] f0 --> v6 v2 --"p:direct/P31"--> c3 v2 --"p:P2583"--> a1 a1 --"p:statement/value/P2583"--> v3 v3 --"wikibase:quantityUnit"--> v4 v3 --"wikibase:quantityAmount"--> v5 bind1[/"if(?unit = 'wd:Q531',?distance,'1^^xsd:integer' / '0^^xsd:integer')if(?unit = 'wd:Q12129',?distance * '3.2616^^xsd:decimal','1^^xsd:integer' / '0^^xsd:integer')if(?unit = 'wd:Q11929860',?distance * '3261.6^^xsd:decimal','1^^xsd:integer' / '0^^xsd:integer')if(?unit = 'wd:Q3773454',?distance * '3261600^^xsd:integer','1^^xsd:integer' / '0^^xsd:integer')'-1^^xsd:integer'"/] v4 --o bind1 v5 --o bind1 bind1 --as--o v6 subgraph s1["http://wikiba.se/ontology#label"] style s1 stroke-width:4px; c9 --"wikibase:language"--> c11 end