Why this wikidata SPARQL query is missing country information? - sparql

This SPARQL query on Wikidata is missing the form of government for a lot of entries. My query:
SELECT DISTINCT ?country ?countryLabel
(group_concat(DISTINCT ?bfogLabel;separator=", ") as ?Government)
WHERE
{
?country wdt:P31 wd:Q3624078.
OPTIONAL {?country wdt:P122 ?bfog } . # basic form of government
SERVICE wikibase:label
{ bd:serviceParam wikibase:language "en" .
?country rdfs:label ?countryLabel .
?bfog rdfs:label ?bfogLabel .
}
}
GROUP BY ?country ?countryLabel
ORDER BY ?countryLabel
Angola is in Wikipedia's infobox: "Unitary dominant-party presidential constitutional republic". But it is empty in this query.
Why is that? More important: is there any fix for this? I saw in this question that wikidata is not as reliable as possible when it comes to data categorization.
Try it out here

Related

How to better filter the sparql query output to avoid the Query timeout limit reached error?

I am getting the Query timeout limit reached error. Is there any way to maybe filter the output more? Thank you!
SELECT (count(distinct ?city) as ?count) WHERE {
?city wdt:P31/wdt:P279* wd:Q486972. # human settlement
?city wdt:P131 ?region.
?city wdt:P17 ?country.
#not a former country
FILTER NOT EXISTS {?country wdt:P31 wd:Q3024240}
#and not an ancient civilisation (needed to exclude ancient Egypt)
FILTER NOT EXISTS {?country wdt:P31 wd:Q28171280}
#not demolished, abolished countries etc.
FILTER NOT EXISTS {?country wdt:P576 ?abolished}
?article schema:about ?city.
?article schema:isPartOf <https://en.wikipedia.org/>.
}

How to get information like capital, currency, language, population about a country in simple way using SPARQL from DBPEDIA

How to get information like capital, currency, language, population about a country in simple way using SPARQL from DBPEDIA.
Is there a simple way to do it?
I found a solution for this problem,
I figured out a query that returns all the required fields,
SELECT DISTINCT ?country ?population ?capital ?currency WHERE {
{?country rdf:type
<http://dbpedia.org/class/yago/WikicatMemberStatesOfTheUnitedNations> .
?country <http://dbpedia.org/ontology/populationTotal> ?population .
?country <http://dbpedia.org/ontology/capital> ?capital .
?country <http://dbpedia.org/ontology/currency> ?currency .}
UNION
{?country rdf:type <http://dbpedia.org/ontology/Country> .
?country <http://dbpedia.org/ontology/populationTotal> ?population .
?country <http://dbpedia.org/ontology/capital> ?capital .
?country <http://dbpedia.org/ontology/currency> ?currency .}
}
SPARQL Result

Wikidata query duplicates

Sorry if my english is bad, but I don't really have any place where I can ask this question in my native language.
I've been trying to create SPARQL query for Wikidata that should create a list of all horror fiction that was created in 1925-1950 years, names of authors and, if available, pictures:
SELECT DISTINCT ?item ?itemLabel ?author ?name ?creation ?picture
WHERE
{
?item wdt:P136 wd:Q193606 . # book
?item wdt:P50 ?author . # author
?item wdt:P577 ?creation .
?item wdt:P577 ?end .
?author rdfs:label ?name .
OPTIONAL{ ?item wdt:P18 ?picture }
FILTER (?creation >= "1925-01-01T00:00:00Z"^^xsd:dateTime) .
FILTER (?end <= "1950-12-31T23:59:59Z"^^xsd:dateTime) .
SERVICE wikibase:label
{
bd:serviceParam wikibase:language "en" .
}
}
However, for some reason this query placing duplicates in the list. DISTINCT doesn't do much. After some time I figured out that the reason is "?item rdfs:label ?name .". If this line is removed, no duplicates are listed. But I need this line to show author name in the list!
Any ideas on how to fix this?
You don't need to use ?item rdfs:label ?name . as you already get items labels as ?itemLabel thank to SERVICE wikibase:label.
Then, you will get duplicate results for every items that have a SELECTed property with possibly multiple values: here, you are SELECTing authors (P50), which will create duplicates for every item with several authors.
The query is actually giving you distinct items. The problem is that some items have multiple rdfs:labels. You can see as an example the item:
SELECT *
WHERE
{
wd:Q2882840 rdfs:label ?label
SERVICE wikibase:label
{
bd:serviceParam wikibase:language "en" .
}
}
And since there are multiple rdfs:label predicates for some items, they are showing up in separate rows.
You can aggregate your results according to the book title (the item's label) using the
group by
keyword.
Thus, every result will be a group which will show up once, and other fields which have different values, will be aggregated using the separator (in this case, a comma).
The fixed query:
SELECT DISTINCT ?item ?itemLabel
(group_concat(distinct ?author;separator=",") as ?author)
(group_concat(distinct ?name;separator=",") as ?name)
(group_concat(distinct ?creation;separator=",") as ?creation)
(group_concat(distinct ?picture;separator=",") as ?picture)
WHERE
{
?item wdt:P136 wd:Q193606 . # book
?item wdt:P50 ?author . # author
?item wdt:P577 ?creation .
?item wdt:P577 ?end .
?author rdfs:label ?name .
OPTIONAL{ ?item wdt:P18 ?picture }
FILTER (?creation >= "1925-01-01T00:00:00Z"^^xsd:dateTime) .
FILTER (?end <= "1950-12-31T23:59:59Z"^^xsd:dateTime) .
SERVICE wikibase:label
{
bd:serviceParam wikibase:language "en" .
}
}
group by ?item ?itemLabel

OR in sparql query

This sparql query on wikidata shows all places in Germany (Q183) with a name that ends in -ow or -itz.
I want to extend this to look for places in Germany and, say, Austria.
I tried modifying the 8th line to something like:
wdt:P17 (wd:Q183 || wd:Q40);
in order to look for places in Austria (Q40), but this is not a valid query.
What is a way to extend the query to include other countries?
Afaik there is no syntax as simple as that. You can, however, use UNION to the same effect like this:
SELECT ?item ?itemLabel ?coord
WHERE
{
?item wdt:P31/wdt:P279* wd:Q486972;
rdfs:label ?itemLabel;
wdt:P625 ?coord;
{?item wdt:P17 wd:Q183}
UNION
{?item wdt:P17 wd:Q40}
FILTER (lang(?itemLabel) = "de") .
FILTER regex (?itemLabel, "(ow|itz)$").
}
or as an alternative create a new variable containing both countries using VALUES:
SELECT ?item ?itemLabel ?coord
WHERE
{
VALUES ?country { wd:Q40 wd:Q183 }
?item wdt:P31/wdt:P279* wd:Q486972;
wdt:P17 ?country;
rdfs:label ?itemLabel;
wdt:P625 ?coord;
FILTER (lang(?itemLabel) = "de") .
FILTER regex (?itemLabel, "(ow|itz)$").
}

getting the list of countries which have more than one official language

I tried to find the all the country from dbpedia which have more than one official language. i tried the following sparql query but did not work.
SELECT distinct ?country ?officialLanguage
WHERE {
?country rdf:type dbo:Country .
?country dbo:officialLanguage ?officialLanguage.
FILTER (COUNT(?officialLanguage) >1)
}
and got the following error-
Virtuoso 37000 Error SP030: SPARQL compiler, line 8: Aggregates are allowed only in result sets at ')' before '>'
I am very new to sparql. I think I am missing something.
As an alternative query to the answer of #svick you could try
SELECT ?country (COUNT(?officialLanguage) AS ?nrOfLanguages)
WHERE {
?country rdf:type dbo:Country .
?country dbo:officialLanguage ?officialLanguage.
}
GROUP BY ?country
HAVING(COUNT(?officialLanguage) > 1)
SPARQL doesn't work like that, it can't deduce that you mean the count of distinct ?officialLanguage for each ?country. You will need to be more explicit than that, for example:
SELECT distinct ?country ?officialLanguage
WHERE {
?country rdf:type dbo:Country .
?country dbo:officialLanguage ?officialLanguage.
{
SELECT ?country COUNT(*) AS ?languages
WHERE {
?country dbo:officialLanguage [].
}
}
FILTER (?languages > 1)
}