How can I prioritize a SPARQL query to query for one thing, before querying another?

43 Views Asked by At

The query below simply goes to Wikipedia and grabs famous people born with the date given. I want the query to first prioritize famous people born in the USA first, then the UK, and then everyone else. This will eventually be for a website whose traffic demographics will be from those countries mainly.

USA = wd:Q6156 Great Britain = wd:Q1833

SELECT ?person ?personLabel ?image ?birthdate ?description WHERE {
  ?person wdt:P31 wd:Q5;
          wdt:P569 ?birthdate.
  OPTIONAL { ?person wdt:P18 ?image. }
  OPTIONAL { ?person schema:description ?description. FILTER(LANG(?description) = "en") }
  FILTER(?birthdate = "${formattedDate}"^^xsd:dateTime)
  SERVICE wikibase:label { bd:serviceParam wikibase:language "[AUTO_LANGUAGE],en". }
}
LIMIT 30

I tried this

SELECT ?person ?personLabel ?image ?birthdate ?description WHERE {
  ?person wdt:P31 wd:Q5;
          wdt:P569 ?birthdate;
          wdt:P27 wd:Q30.
  OPTIONAL { ?person wdt:P18 ?image. }
  OPTIONAL { ?person schema:description ?description. FILTER(LANG(?description) = "en") }
  FILTER(?birthdate = "${formattedDate}"^^xsd:dateTime)
  SERVICE wikibase:label { bd:serviceParam wikibase:language "[AUTO_LANGUAGE],en". }

  FILTER (?birthPlace = wd:Q6156) UNION
  FILTER (?birthPlace = wd:Q1833) UNION
  ?person
}
LIMIT 30

but the query is somehow malformed. Not sure what I'm doing wrong.

0

There are 0 best solutions below