Count column values in SPARQL - sparql

I am writing a query in SPARQL for a knowledge graph.
I have a column having Country and names of countries repeating.
For example: Japan, India, US, India, India, US, US, Romania etc. and so on.
I need to write a SPARQL query that gives me the count of each country in that column.
Example: Japan 1, India 3, US 3 etc. and so on.
SELECT ?countryName COUNT(?CountryName)
WHERE
{
...
}
GROUP BY ?countryName
HAVING (?countryName = "Germany")

Try something like this:
SELECT ?countryName COUNT(*) AS ?count
WHERE
{ ?iri a OBJECT .
?iri rdfs:label ?countryName .
}
GROUP BY ?countryName
Keep in mind that you will need to replace OBJECT with the entity type, and I have assumed you are using the rdfs:label predicate for the labels.

Related

Wikidata Query misses results

I am trying to query all the countries from Wikidata (query link):
SELECT ?item WHERE { ?item wdt:P31 wd:Q6256. }
Unfortunately, the results are missing for example Switzerland (Q39):
https://www.wikidata.org/wiki/Q39
Looking at the Switzerland data, it has the triple: instance of (P31) country (Q6256).
Could you help me understand why Q39 is not present in the results then?
Thanks!
In WikiData, often you have what are called statements. These allow for qualifications to those statements.
For instance, in the case of Switzerland being a country, the qualification is that the rank of this statement should be 'normal' instead of preferred.
The preferred way to refer to Switzerland is sovereign state (wd:Q3624078), and it looks like WikiData will only have a wdt:P31 relationship between an entity and its 'preferred rank' classification only, as this query shows.
I think this could be because 'country' is a more generic concept, e.g. Wales is a country but not a sovereign state.
Fear not however, as this query:
SELECT DISTINCT ?item
WHERE {
?item p:P31/ps:P31 wd:Q6256.
}
returns wd:Q39 as well. What this query is doing is to navigate from Switzerland to Country via the statement.
That is, we have in our data:
wd:Q39 p:P31 wds:Q39-fbe1ac75-4a8a-93c4-6009-81055d79f9cb .
wds:Q39-fbe1ac75-4a8a-93c4-6009-81055d79f9cb ps:P31 wd:Q6256 .
but not:
wd:Q39 wdt:P31 wd:Q6256 .
Try this:
SELECT DISTINCT *
WHERE {
?item p:P31 ?y .
?y ps:P31 wd:Q6256 ;
?p ?o .
VALUES ?item {wd:Q39}
}
to see for yourself.

Wikidata SPARQL query is missing part of expected results

On Wikidatas SPARQL endpoint i tried to find all cities worldwide with a population greater than 100000.
I'm getting lots of correct results with my query. But when i checked for some particular cities they didn't show up in the list.
My query:
SELECT DISTINCT ?cityLabel ?population ?coord ?countryLabel ?shortCountry ?city WHERE {
?city (wdt:P31/(wdt:P279*)) wd:Q515;
wdt:P1082 ?population;
wdt:P625 ?coord.
FILTER(?population > 100000 )
?city wdt:P17 ?country.
?country wdt:P298 ?shortCountry.
SERVICE wikibase:label { bd:serviceParam wikibase:language "en". }
}
ORDER BY ASC (?shortCountry)
I looked for the following cities in the results:
Berlin
Beirut
Lyon
But they didn't show up.
Try it out here
Ok i figured something out for myself:
I dug a little deeper into the wikidata object of the cities i mentioned and found out, that they are no instance of "city" (Q515).
Berlin belongs to e.g. "capital", "city with millions of inhabitants", "metropolis"
Lyon belongs to "commune of france" and "big city"
Beirut belongs to e.g. "capital" and "big city"
But none of them is listed as a "city". That's for sure not intentionally because other bigger cities are also an instance of "city" e.g. Paris
So in conclusion:
Check all the categories your filtering for and check your expected results to match them.

Retrieve the US release date for a movie from Wikidata using Sparql

I am trying to retrieve the titles and release dates (publication date) for movies using the wikidata.org sparkql endpoint (https://query.wikidata.org/). The titles are listed in different languages, which are filtered in the query below. However, some movies also have several publication dates (e.g. for different countries), e.g. https://www.wikidata.org/wiki/Q217020. I'm not sure how the RDF triple structure is actually used to assign a country to the value of another triple, but specifically, how can I only retrieve the publication date for a movie in the US?
SELECT ?item ?title ?publicationdate
WHERE {
?item wdt:P31 wd:Q11424 ;
rdfs:label ?title ;
wdt:P577 ?publicationdate ;
filter ( lang(?title) = "en" )
}
ORDER BY ?movieid
LIMIT 10
Solution
The solution provided by M.Sarmini works. Apparently, facts such as publication data are stored as n-ary relations, they create a unique symbolic tag that links the resources. The value that P577 links to is just the date, when turned into a string will give the release date, while in reality it is a token that you can link to other qualifiers.
Just add a new variable to hold the place of publication and filter your results to just list US films like this:
PREFIX q: <http://www.wikidata.org/prop/qualifier/>
PREFIX s: <http://www.wikidata.org/prop/statement/>
SELECT distinct ?item ?title ?publicationdate
WHERE {
?item wdt:P31 wd:Q11424;
rdfs:label ?title;
p:P577 ?placeofpublication.
?placeofpublication q:P291 wd:Q30.
?placeofpublication s:P577 ?publicationdate;
filter ( lang(?title) = "en")
}
ORDER BY ?item

Query DBpedia to get abstract for different inputs

I have a question I need to build a single query to DBpedia such that, If I give any one of these as input like a City name or a person name or a Institute name or a Instrument name can I get its abstract as a output???
For instance,
New York- New York is a state in the Northeastern and Mid-Atlantic regions of the United States......
Mars- Mars is the fourth planet from the Sun and the second smallest planet in the Solar System....
Michael Jackson- Michael Joseph Jackson was an American singer, songwriter, dancer, and actor......
I have tried but its not working for all.
SELECT ?abstract WHERE {
<http://dbpedia.org/resource/New_York>
<http://dbpedia.org/ontology/abstract>
?abstract
FILTER langMatches(lang(?abstract), "en")
}
If you intend to get the abstract for multiple things, supply those multiple things within a VALUES block. I found that matching by ?name worked sufficiently well for name-based searches.
SELECT DISTINCT ?abstract WHERE {
[ rdfs:label ?name
; dbpedia-owl:abstract ?abstract
] .
FILTER langMatches(lang(?abstract),"en")
VALUES ?name { "New York"#en }
}
LIMIT 10

Sparql queries Sparql queries

I need to query dbpedia using sparql:
input: name of a person
output:
the surname, sex , age, occupation
input: an organization name
output:
the founder, if it is public or private
input: a date
output:
if it is an important date for Example: christians, or Easter
input: a city name
output:
the state, region of the city.
Here is an example for #4
First you need to look at the Ontology Class of City in DbPedia.
city is described as city
Select distinct ?city ?location
Where
{
?city a dbo:City.
?city dbo:location ?location.
}
LIMIT 100
Then you can the filter clause to get the city you want.
i think i need to know the resource first but for example
for the 1. i can do ...
SELECT *
where
{
<http://dbpedia.org/resource/Name_x> dc:description ?p.
<http://dbpedia.org/resource/Name_x> foaf:surname ?y
<http://dbpedia.org/resource/Name_x> foaf:name ?y
<http://dbpedia.org/resource/Name_x> foaf:office ?y
}
im checking the others