TL;DR
How to query (sparql) about properties of a property?
Or..
So as part of my project I need to find the properties in wikidata that have any time constraint, to be specific both "start time" and "end time".
I tried this query:
SELECT DISTINCT ?prop WHERE {
SERVICE wikibase:label { bd:serviceParam wikibase:language "[AUTO_LANGUAGE],en". }
?person wdt:P31 wd:Q5.
?person ?prop ?statement.
?statement pq:P580 ?starttime.
?statement pq:P582 ?endtime.
}
LIMIT 200
**yeah the properties should be related to humans
Anyway, I do get some good results like:
http://www.wikidata.org/prop/P26
http://www.wikidata.org/prop/P39
But I also get some other properties that definitely wrong.
so, basically what i'm trying to do is to get a list of properties that has the property constraint (P2302) of- allowed qualifiers constraint (Q21510851) with Start time (P580) and End Time (P582)
is that even possible:
I tried some queries like:
SELECT DISTINCT ?property ?propertyLabel ?propertyDescription ?subpTypeOf ?subpTypeOfLabel
WHERE
{
?property rdf:type wikibase:Property .
?property wdt:P2302 ?subpTypeOf.
SERVICE wikibase:label { bd:serviceParam wikibase:language "en" }
}
but does not get the results I wanted.
is it even possible to query this kind of stuff?
Thanks
Qualifiers are used on property pages too. Your second query should be:
SELECT DISTINCT ?prop ?propLabel {
?prop p:P2302 [ ps:P2302 wd:Q21510851 ; pq:P2306 wd:P580, wd:P582 ] ;
p:P2302 [ ps:P2302 wd:Q21503250 ; pq:P2308 wd:Q5 ; pq:P2309 wd:Q21503252 ] .
SERVICE wikibase:label { bd:serviceParam wikibase:language "[AUTO_LANGUAGE],en" }
} ORDER BY ASC(xsd:integer(strafter(str(?prop), concat(str(wd:), "P"))))
Try it!
Your first query is correct, but note that this is an 'as-is' query. For example, wd:P410 does not have respective constraints, but look at wd:Q83855.
Related
Like in the SQL aggregate MAX, MIN or FIRST, it gets only one value, not duplicating lines.
Real Wikidata case
Where the OPTIONAL clause expands from 253 to 257 lines:
# Countries and its codes
SELECT ?code ?item ?itemLabel ?osmId
WHERE
{
?item wdt:P297 ?code.
OPTIONAL{?item wdt:P402 ?osmId .}
SERVICE wikibase:label { bd:serviceParam wikibase:language "en". }
}
ORDER BY ?code
try here
I need only one (any) osmId. How to do something like FIRST{OPTIONAL{?item wdt:P402 ?osmId .}} ?
NOTES:
it is not a duplicate of How to get only the most recent value from a Wikidata property?
it is not a duplicate of Why does this Wikidata SPARQL query only work for the first element in a list?
... no exactly need for simple "any first".
Here a WIKI answer (please you can edit to enhance this answer!)
# Countries and its codes
SELECT ?code ?item ?itemLabel
(MAX(?osmId) as ?osmId_max) (COUNT(?code) as ?osmId_n)
WHERE
{
?item wdt:P297 ?code.
OPTIONAL{?item wdt:P402 ?osmId .}
SERVICE wikibase:label { bd:serviceParam wikibase:language "en". }
}
GROUP BY ?code ?item ?itemLabel
ORDER BY ?code
try
The COUNT(?code) is only to check the lines where osmId was not an Unique-ID.
Other simple solution to filter only the first option?
Using SAMPLE
As the #ValerioCocchi suggestion, we can use SAMPLE instead MAX:
SELECT ?code ?item ?itemLabel (SAMPLE(?osmId) as ?osmId_sample)
WHERE
{
?item wdt:P297 ?code.
OPTIONAL{?item wdt:P402 ?osmId .}
SERVICE wikibase:label { bd:serviceParam wikibase:language "en". }
}
GROUP BY ?code ?item ?itemLabel
ORDER BY ?code
try
SAMPLE use a little bit less CPU-time, but the main motivation to use is when you don’t care which value is returned. In the case of Wikidata, when the property-value is to be unique but there are some (minimal) errors, and you can ignore them.
NOTE about the osmId: the advantage of MAX in this particular query, using an numeric ID related to a temporal sequence, is that it can be a "fresher" ID... But in OpenStreetMap (OSM) the strategy can be the inverse: most old is the most stable ID. So, SAMPLE make sense also in a context of ignorance about better strategy.
Using FILTER
The #StanislavKralin suggestion:
SELECT ?code ?item ?itemLabel ?osmId
WHERE
{
?item wdt:P297 ?code.
OPTIONAL{
?item wdt:P402 ?osmId
FILTER NOT EXISTS {
?item wdt:P402 ?osmId, ?osmId_ .
FILTER (?osmId_ > ?osmId)
}
}
SERVICE wikibase:label { bd:serviceParam wikibase:language "en". }
}
ORDER BY ?code
try
Seems more verbose.
I would like to access child properties of wikidata entities. An example may be property P1033, which is a child of P4952, for an entity such as Q49546. How can I do this dynamically in a SPARQL query?
Using the query builder provided by the online Wikidata Query Service, I can construct a simple query, which works for normal properties (in the linked example: mass), but not for the desired sub-properties (in the linked example: NPFA-code for health hazard), which end up empty, even though they are clearly set in the web-result. Side-note: it is a different example than the one from the first paragraph.
The desired objective is the dynamic query as follows:
SELECT ?p ?item ?itemDescription ?prop ?value ?valueLabel ?itemLabel ?itemAltLabel ?propLabel WHERE {
BIND(wd:Q138809 AS ?item)
?prop wikibase:directClaim ?p.
#?item ?p ?value.
SERVICE wikibase:label { bd:serviceParam wikibase:language "[AUTO_LANGUAGE],en".
?value rdfs:label ?valueLabel.
?prop rdfs:label ?propLabel.
?item rdfs:label ?itemLabel;
skos:altLabel ?itemAltLabel;
schema:description ?itemDescription.
}
}
ORDER BY DESC(?prop)
LIMIT 10
With the line 4 as a comment, I can get my propLabel as desired, but no value; doing it the other way round with the line not as comment, I do get only the properties, which are set on first level, but not the child properties.
Thanks to #AKSW, I herewith post the final query solving my problem:
SELECT ?item ?itemLabel ?itemDescription ?itemAltLabel ?prop ?propertyLabel ?propertyValue ?propertyValueLabel ?qualifier ?qualifierLabel ?qualifierValue
{
VALUES (?item) {(wd:Q138809)}
?item ?prop ?statement .
?statement ?ps ?propertyValue .
?property wikibase:claim ?prop .
?property wikibase:statementProperty ?ps .
OPTIONAL { ?statement ?pq ?qualifierValue . ?qualifier wikibase:qualifier ?pq . }
SERVICE wikibase:label { bd:serviceParam wikibase:language "en" }
}
The key step for me was to understand that child properties are actually called qualifiers.
I am trying to make a sparql query inside another sparql query. In sql, we can do it like this:
SELECT column_name(s)
FROM table_name
WHERE column_name IN (SELECT STATEMENT);
I want to do the same thing in SPARQL query. Specifically I have two sparql queries and I want to combine them together. My end goal is to find 'Siemens PLM Software Company's subsidiaries. In order to do this, first i need to find the company's id and then look for its subsidiaries.
Q1: Finds the unique identity of 'Siemens PLM Software Company'
SELECT DISTINCT ?item ?label ?articleLabel WHERE {
?item ?label "Siemens PLM Software"#en;
wdt:P31 wd:Q4830453.
?article schema:about ?item;
schema:inLanguage "en".
SERVICE wikibase:label { bd:serviceParam wikibase:language "en". }
}
Returns Q15898201
Q2: Find the subsidiary of 'Siemens PLM Software Company'
SELECT ?Subsidiary ?SubsidiaryLabel ?parent_organization ?parent_organizationLabel WHERE {
SERVICE wikibase:label { bd:serviceParam wikibase:language "[AUTO_LANGUAGE],en". }
?Subsidiary wdt:P749 wd:Q15898201.
OPTIONAL { ?Subsidiary wdt:P749 ?parent_organization. }
Returns Siemens
I would like to combine them together to something like this:
SELECT ?Subsidiary ?SubsidiaryLabel ?parent_organization ?parent_organizationLabel WHERE {
SERVICE wikibase:label { bd:serviceParam wikibase:language "[AUTO_LANGUAGE],en". }
?Subsidiary wdt:P749 wd:
{SELECT DISTINCT ?item ?label ?articleLabel WHERE {
?item ?label "Siemens PLM Software"#en;
wdt:P31 wd:Q4830453.
?article schema:about ?item;
schema:inLanguage "en".
SERVICE wikibase:label { bd:serviceParam wikibase:language "en". }
}
}.
OPTIONAL { ?Subsidiary wdt:P749 ?parent_organization. }
Do you have any idea how can i do this? Thank you!
There were several things not quite clear:
Did you really mean to return the rdfs:label property as ?label? I don't understand why you'd want to do that, so I assume you mean to just directly match on the rdfs:label property without returning it
Why return ?articleLabel? It's not a real label anyway but just the value of ?label as a literal. I assume you mean to just return ?article.
It seems you have the parent organization relationship backwards? Your Q2 asks for subsidiaries of Siemens PLM Software. None exist. To ask for Siemens, you need to ask for
wd:Q15898201 wdt:P749 ?parent_organization
and not
?Subsidiary wdt:P749 wd:Q15898201
With that out of the way: There is no need for a subquery here. The query can be achieved simply by writing out the graph pattern for the desired graph structure, using OPTIONAL for parts that may not exist, and making sure that the variable names match up correctly throughout the query:
SELECT ?item ?itemLabel ?parent_organization ?parent_organizationLabel WHERE {
# Find business by label
?item rdfs:label "Siemens PLM Software"#en;
wdt:P31 wd:Q4830453.
# Find English-language articles about the business
?article schema:about ?item;
schema:inLanguage "en".
# Find the business' parent organization, if one exists
OPTIONAL { ?item wdt:P749 ?parent_organization. }
# For any variable ?xxx, add variable ?xxxLabel with the label
SERVICE wikibase:label { bd:serviceParam wikibase:language "[AUTO_LANGUAGE],en". }
}
That being said, you can use a subquery if you really want to:
SELECT ?item ?itemLabel ?parent_organization ?parent_organizationLabel WHERE {
{
SELECT ?item {
?item rdfs:label "Siemens PLM Software"#en;
wdt:P31 wd:Q4830453.
}
}
?article schema:about ?item;
schema:inLanguage "en".
OPTIONAL { ?item wdt:P749 ?parent_organization. }
SERVICE wikibase:label { bd:serviceParam wikibase:language "[AUTO_LANGUAGE],en". }
}
This is equivalent to the first query, but runs much slower because the query optimiser is not as good at handling subqueries.
I would like to know how to query Wikidata by using the alias ("also known as").
Right now I am trying
SELECT ?item
WHERE
{
?item rdfs:aliases ?alias.
FILTER(CONTAINS(?alias, "Angela Kasner"#en))
}
LIMIT 5
This is simply a query that works if I replace rdfs:aliases by rdfs:labels.
I am trying this, because Help:Aliases says that aliases are searchable in the same way as labels, but I can't find any other resource on that nor can I find an example.
This query might be helpful for someone querying also known as for properties:
SELECT ?property ?propertyLabel ?propertyDescription (GROUP_CONCAT(DISTINCT(?altLabel); separator = ", ") AS ?altLabel_list) WHERE {
?property a wikibase:Property .
OPTIONAL { ?property skos:altLabel ?altLabel . FILTER (lang(?altLabel) = "en") }
SERVICE wikibase:label { bd:serviceParam wikibase:language "en" .}
}
GROUP BY ?property ?propertyLabel ?propertyDescription
LIMIT 5000
This SPARQL request looks for all cities called "Berlin" in Wikidata:
SELECT DISTINCT ?item ?itemLabel ?itemDescription WHERE {
?type (a | wdt:P279) wd:Q515. # Sub-type of city
?item wdt:P31 ?type.
?item rdfs:label "Berlin"#en.
SERVICE wikibase:label { bd:serviceParam wikibase:language "en". }
}
PROBLEM: It returns zero result.
Meanwhile, the request below correctly finds Q64 (capital and city-state of Germany), but it also returns a lot of other things called Berlin, so I want to filter on cities (then in a future phase I will order these cities by population, but that is outside the scope of this question):
SELECT DISTINCT ?item ?itemLabel ?itemDescription WHERE {
?item rdfs:label "Berlin"#en.
SERVICE wikibase:label { bd:serviceParam wikibase:language "en". }
}
Note: My code for getting instances of subclasses of city (Berlin is a big city which is subclass of city) seems to work correctly, as illustrated by the results of this query.
It was a Wikidata bug.
According to Wikidata's Jura1, it was a bug in Wikidata caused by someone's experiments with "preferred rank".
Discussion at https://www.wikidata.org/wiki/Wikidata:Project_chat/Archive/2016/09#P31_inconsistency
The bug has been fixed just now.
You can only query for data that is contained in the dataset.
If you try an alternative of your query
SELECT DISTINCT ?item ?itemLabel ?itemDescription ?type1 ?type2 WHERE {
?item rdfs:label "Berlin"#en.
optional{?item rdf:type ?type1 }
optional{?item wdt:P279 ?type2 }
SERVICE wikibase:label { bd:serviceParam wikibase:language "en". }
}
it returns no types, neither connected by rdf:type nor wdt:P279.
If you have a look at the entity of the capital and city state Berlin, you can see that there is information about "instance of", but this property is supposed to be https://www.wikidata.org/wiki/Property:P31. And none of them links to wd:Q515, I'm wondering from where you got this idea.
But to be honest, I don't know that much about Wikidata and to me, it's not clear why no rdf:type is used, but a common pattern for RDF datasets is to use
?s rdf:type/rdfs:subClassOf* SUPER_CLASS .
if we assume that there is rdf:type information available.
If you check the types wd:Q64 is an instance of
SELECT DISTINCT ?type ?typeLabel WHERE {
wd:Q64 (a | wdt:P31) ?type.
SERVICE wikibase:label { bd:serviceParam wikibase:language "en". }
}
ORDER BY ?item
None of them are City (wd:Q515) or a sub-class of it.
Looks like a data issue. Perhaps you should contact Wikidata.