Wikidata query to get all values for specific properties - sparql

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

Related

How to relate p[s[n]] and w[d[t]] properties in wikidata sparql query?

I am trying to get all quantitative values of a given wd:* entry including the statements details (qualifiers?) - foremost the unit, since most numeric values are useless without it.
I was able to come up with the following query to get all quantitative values:
SELECT ?p ?property ?propertyLabel ?propertyDescription ?v WHERE {
wd:Q1726 ?p ?v.
?property wikibase:propertyType wikibase:Quantity;
wikibase:directClaim ?p.
SERVICE wikibase:label { bd:serviceParam wikibase:language "[AUTO_LANGUAGE],en". }
}
ORDER BY ?v
p
property
propertyLabel
propertyDescription
v
wdt:P2046
wd:P2046
area
area occupied by an object
310.71
As you can see this is perfect to get the value of 310 kmĀ² as well as the labels and description of property you are looking for.
With the knowledge of the property being wd[t]:2046, I came up with a new query to get the value with quantifiers for it's best statement:
SELECT ?statement ?value ?valueLabel ?unit ?unitLabel WHERE {
wd:Q1726 p:P2046 ?statement.
?statement psn:P2046 ?valuenode.
?valuenode wikibase:quantityAmount ?value.
?valuenode wikibase:quantityUnit ?unit.
?statement a wikibase:BestRank.
SERVICE wikibase:label { bd:serviceParam wikibase:language "en". }
}
statement
value
valueLabel
unit
unitLabel
wds:Q1726-33fec614-441a-a6c4-06cd-b79490a52c4b
310710000
310710000
wd:Q25343
square metre
However I have no idea how to join these two queries to relate ?v of the first one with ?statement of the second one. In the end what I'm looking for is to add a unit column to the first query. All examples and explanations I've seen regarding quantifiers are searching for a given property and I'm struggling to understand how to relate statement with statement/value-normalized/ and statement/value/ or how to filter for a property to be one of those.

Wikidata Returning Multiple Results For Id

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". }
}

Get a value even if the person gender is not available in wiki data sparql query

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". }
}

Questions on Filter of Monolingual Text

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

Bad aggregate when adding extra items to select wikidata sparql

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?