I send a query as below. Without filter, I got responses and in the response, I checked the values are there. but with the filter statement, I failed to get query. What's wrong with my query?
P1813 property type is a Monolingual text, so I used the filter statement as below.
SELECT DISTINCT ?i ?iLabel ?v1
WHERE {
?i wdt:P31 wd:Q7603.
?i wdt:P1813 ?v1.
FILTER(?v1 = "GNU AGPL"#en) #commenting out this statement, I got responses.
SERVICE wikibase:label { bd:serviceParam wikibase:language "en,[AUTO_LANGUAGE]". }
}
LIMIT 100
Related
I have a list of properties, let's say P8839, P21, P91 and P1039.
I'd like to query Wikidata and get all unique value labels that belong to these properties.
SELECT ?itemLabel WHERE {
?itemLabel wdt:P8839 ?cui.
SERVICE wikibase:label { bd:serviceParam wikibase:language "en". }
}
What I have returns ids of one page where the property is included. I'm rather new to queries so I'm not sure what needs to change to include all properties and unique labels.
This query lists the distinct English labels of all values of the properties listed in VALUES:
SELECT DISTINCT ?valueLabel
WHERE {
VALUES (?property) {
(wdt:P1039)
(wdt:P8839)
#(wdt:P21)
#(wdt:P91)
}
?s ?property ?value.
SERVICE wikibase:label { bd:serviceParam wikibase:language "en". }
}
(Note that you get a "Query timeout limit reached" in WDQS if you include all four properties, which is why I commented out the last two. Unless you can add further restrictions, you might have to run the query multiple times for different properties.)
If you also want to get a link to the actual values, you can add ?value to the SELECT:
SELECT DISTINCT ?value ?valueLabel
I am trying to construct a query to return a list of actors and their spouses while including marriage and divorce dates for each couple. So I would expect to see each actor duplicate with each instance of a new relationship... however when I try and include the start time and end time properties in the query, I am getting duplicate results. I suspect this is because the "name" of the spouses and the is stored in a different wikidata prefix and I'm not grouping them correctly.
Here is a sample query:
SELECT ?person ?personLabel ?spouse ?spouseLabel ?starttime ?endtime
WHERE
{
?person wdt:P106 wd:Q33999, wd:Q2526255, wd:Q28389, wd:Q3282637;
wdt:P26 ?spouse.
?person p:P26 [pq:P580 ?starttime; pq:P582 ?endtime].
SERVICE wikibase:label { bd:serviceParam wikibase:language "en". }
}
ORDER BY ASC(UCASE(str(?personLabel)))
LIMIT 10
here is a link to the sparql interactive service so you can see the duped results I'm referring to:
https://query.wikidata.org/#SELECT%20%3Fperson%20%3FpersonLabel%20%3Fspouse%20%3FspouseLabel%20%3Fstarttime%20%3Fendtime%0AWHERE%0A%7B%0A%20%20%3Fperson%20wdt%3AP106%20wd%3AQ33999%2C%20wd%3AQ2526255%2C%20wd%3AQ28389%2C%20wd%3AQ3282637%3B%0A%20%20%20%20%20%20%20%20%20%20wdt%3AP26%20%3Fspouse.%0A%20%20%3Fperson%20p%3AP26%20%5Bpq%3AP580%20%3Fstarttime%3B%20pq%3AP582%20%3Fendtime%5D.%0A%20%20SERVICE%20wikibase%3Alabel%20%7B%20bd%3AserviceParam%20wikibase%3Alanguage%20%22en%22.%20%7D%0A%7D%0AORDER%20BY%20ASC%28UCASE%28str%28%3FpersonLabel%29%29%29%0ALIMIT%2010%0A
screencap of duped results
The problem with your query is that there was no link between the spouse and the statement about their marriage.
So for every actor, you are returning all their spouses, and also all the start/end dates of their marriages, regardless of whether they relate to the specific spouse.
What you need to do is to use the ps: namespace, like so:
SELECT ?person ?personLabel ?spouse ?spouseLabel ?starttime ?endtime
WHERE
{
?person wdt:P106 wd:Q33999, wd:Q2526255, wd:Q28389, wd:Q3282637 .
?person p:P26 [ ps:P26 ?spouse ; #This is the necessary change.
pq:P580 ?starttime;
pq:P582 ?endtime ].
SERVICE wikibase:label { bd:serviceParam wikibase:language "en". }
}
ORDER BY ASC(?personLabel)
LIMIT 10
In general, the wdt: namespace is for linking entities directly, the p: namespace links an entity to a statement, ps: links a statement to an entity, and pq: tells us something about the statement.
I am trying to get the label of a wikidata item using its id. My sparql looks like:
SELECT ?label
WHERE
{
wd:Q245068 rdfs:label ?label .
FILTER (langMatches( lang(?label), "EN" ) )
}
When I run this, I get 3 results that are all "comedian".
Why does this return multiple results for the same language/id?
Yes, that's funny...
The reason is obvious if you just check the item
"English" means three different things (here labeled in German, the international language of high comedy): Canadian, British, and American English. You get all three.
Here's how to avoid it:
SELECT DISTINCT ?label
WHERE {
wd:Q245068 rdfs:label ?label .
FILTER (langMatches( lang(?label), "EN" ) )
}
Or, use a more specific language code:
FILTER (langMatches( lang(?label), "EN-GB" ) )
But that runs the risk of not returning any label if it isn't set in the particular variety you've chosen. You can get around that, but really at the end you are just reimplementing the standard service that exists for exactly this purpose:
# just ad "Label" to get the label in the SELECT
SELECT ?item ?itemLabel
WHERE {
?item wet:P31 Q5.
# WITH THIS SERVICE
SERVICE wikibase:label { bd:serviceParam wikibase:language "en". }
}
This is a simple SPARQL query that gives the person gender. Now what I wanted to know that if the person gender is not given, can we get a value 0 or something like 'yes' or 'no' instead of an empty cell. The reason I want to do this is that I have some QID's in which some have gender and some don't. When I implement it in Python the query doesn't give result for all.
SELECT DISTINCT ?genderLabel ?person ?personLabel
WHERE {
# set ?item to Person
BIND(wd:Q11831 AS ?person)
OPTIONAL { ?person wdt:P21 ?gender. }
# set language for labels
SERVICE wikibase:label { bd:serviceParam wikibase:language "en". }
}
I am looking to retrieve data for a given location.
So using the below I am able to retrieve the bordering locations of France and Scotland.
SELECT (GROUP_CONCAT(?borderLabel;separator=",") AS ?borders)
WHERE {
?location wdt:P47 ?border.
?location wdt:P2046 ?area.
?location wdt:P1082 ?population.
FILTER (?location=wd:Q142 || ?location=wd:Q22)
SERVICE wikibase:label {
bd:serviceParam wikibase:language "en".
?border rdfs:label ?borderLabel
}
}
GROUP BY ?location
But as soon as I add anything to the SELECT, ie. SELECT ?locationLabel (GROUP_CONCAT(?borderLabel;separator=",") AS ?borders) it tells me Query is malformed: Bad aggregate
I am however able to add ?location to return the wd entity value without issue.
What is wrong here?