Query all instance of and subclasses of wd:43229 - sparql

I would like to fetch all entities that lead to wd:43229. However, my query, for a particular example (wd:Q95) returns partially correct results and also redundant entities.
SELECT distinct ?subClass ?subClassLabel
WHERE {
wd:Q95 p:P31/ps:P31/wdt:P279* ?subClass .
?subClass p:P31/ps:P31/wdt:P279* ?node .
?subClass rdfs:label ?subClassLabel.
OPTIONAL {?node p:P31/ps:P31/wdt:P279* wd:Q43229 }
FILTER( LANG(?subClassLabel)="en" )
}
List of entities required
business
technology company
public company
juridicial person
enterprise
operation
organizational unit
public company
joint-stock company
limited company
commercial company
private company
company
Any idea how to fix this query?

Related

Retrieving Covid-19 pandemic statistics per country from DBpedia

I'm trying to get the arrival date, the confirmed and recovery cases total and the deaths total of Covid-19 pandemic per country from DBpedia, using this query:
PREFIX dbp: <http://dbpedia.org/property/>
SELECT distinct ?country ?arrivalDate ?confirmedCases ?recoveryCases ?deaths WHERE {
?country a dbp:location;
dbp:arrivalDate ?arrivalDate;
dbp:confirmedCases ?confirmedCases;
dbp:recoveryCases ?recoveryCases;
dbp:deaths ?deaths
}
Unfortunately, it doesn't return anything
?country a dbp:location
With this triple pattern, you are trying to find entities that have http://dbpedia.org/property/location as type (rdf:type). This is not what you intend, because
dbp:location is a property (not a class), and
in the subject position, you don’t seem to want to find locations, but information about the pandemic.
So ideally rename ?country to something like ?pandemicInfo (for clarity), and then ask for the dbp:location of that ?pandemicInfo:
SELECT DISTINCT ?pandemicInfo ?country ?arrivalDate ?confirmedCases ?recoveryCases ?deaths
WHERE {
?pandemicInfo
dbp:location ?country ;
dbp:arrivalDate ?arrivalDate ;
dbp:confirmedCases ?confirmedCases ;
dbp:recoveryCases ?recoveryCases ;
dbp:deaths ?deaths .
}
To only get information about the COVID-19 pandemic, you could add:
dbo:disease dbr:COVID-19
And if there is a type that all entities share, e.g., dbo:Pandemic, you could add:
a dbo:Pandemic
(But you should verify if all the entities you are interested in contain these statements, otherwise you would exclude them.)

How To Efficiently Design Nested OPTIONAL Clauses In SPARQL

I want to retrieve data that has optional elements at multiple levels. For example, assume I have four ancestors - Fred, Sam, George, and Mark. Fred and Sam have kids ... George and Mark do not. All of Fred's kids have nicknames, but two of Sam's four kids do not.
I want to query all of the kids of my ancestors and return their names, ages, and nicknames.
It seems like this would work:
SELECT DISTINCT ?token ?ancestorName ?childName ?childAge ?childNickname
WHERE
{
FILTER ( ?token IN ("Fred","Sam","George","Mark") )
?ancestor foo:name ?token .
?ancestor foo:fullname ?ancestorName .
OPTIONAL
{
?ancestor foo:parentOf ?child .
?child foo:fullname ?childName .
?child foo:age ?childAge .
OPTIONAL { ?child foo:nickname ?childNickname }
}
}
Everything seems to work fine if an ancestor doesn't have a child ... all of the outer optional clause returns quickly
with no data. If the ancestor has children, and each has a nickname, it returns quickly and fills in the data. The problem seems to happen when the ancestor has a child, but the child does not have a nickname.
It works ... but it takes a very long time (I have lots of data). It appears that the inner OPTIONAL clause
OPTIONAL { ?child foo:nickname ?childNickname }
... does a cross product ... combining every ?child with every ?childNickname ... and then returns the right value.
How can I write this SPARQL SELECT to run efficiently (not do a cross product) and return all of the ancestors and all of the kids even if a kid doesn't have a nickname? I've tried FILTERS. I've tried checking whether ?child was BOUND. I haven't found the secret to make it run quick.
Thanks for the help!

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

Wikidata Sparql: how to get a list of actors that where never directed by a given person?

Let's say I want a list of actors that were never directed by Tim Burton among a list of popular movies.
I tried to do it with this steps:
Select all actors that Tim Burton ever directed (sub select)
Select a list of actors from a list of popular movies (by imdb ids)
Exclude all actors from the first selection in the second selection (NOT IN)
Here is a code I tried that do not works (the NOT IN fail, I don't know why):
SELECT DISTINCT ?actor ?actorLabel
WHERE {
?film wdt:P31 wd:Q11424
;wdt:P161 ?actor
;wdt:P345 ?imdbId .
{
SELECT ?excludeActors
WHERE {
?film wdt:P31 wd:Q11424
; wdt:P57 wd:Q56008
; wdt:P161 ?excludeActors .
}
} .
FILTER(?actor NOT IN (?excludeActors)) .
FILTER(?imdbId = "tt1077368" || ?imdbId = "tt0167260") .
SERVICE wikibase:label { bd:serviceParam wikibase:language "fr" }
}
Or follow this link
(there is a filter on Christopher Lee that you can remove [last one], it is used to highlight what I explain here:)
In this code I have two movies: Dark Shadows (directed by Tim Burton) and The Lord of the Rings 3. In this example Christopher Lee is present in both movies, which means he should be excluded since Tim Burton directed him in Dark Shadows.
You can see that he his in the list.
I really don't understand why the NOT IN fail with the sub select. I tried the sub Select request and I found Christopher Lee inside which means he should be excluded.
If I understood correctly, you want all actors that acted in the given movies, but have never acted in any movie directed by Tim Burton. I would use FILTER NOT EXISTS:
SELECT DISTINCT ?actor ?actorLabel
WHERE {
VALUES ?imdbId { "tt1077368" "tt0167260" }
?film wdt:P31 wd:Q11424
;wdt:P161 ?actor
;wdt:P345 ?imdbId .
FILTER NOT EXISTS {
[] wdt:P31 wd:Q11424
; wdt:P57 wd:Q56008
; wdt:P161 ?actor .
}
SERVICE wikibase:label { bd:serviceParam wikibase:language "fr" }
}
LIMIT 100

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