Project:SPARQL query service/queries/examples: Difference between revisions
From SCDB
No edit summary |
(→Ships) |
||
(20 intermediate revisions by the same user not shown) | |||
Line 2: | Line 2: | ||
These basic queries help to understand [[Wikidata:Wikidata:A beginner-friendly course for SPARQL|SPARQL]] and the Wikibase RDF format. | These basic queries help to understand [[Wikidata:Wikidata:A beginner-friendly course for SPARQL|SPARQL]] and the Wikibase RDF format. | ||
=== Ships === | === Ships with manufacturer === | ||
{{SPARQL2|query=SELECT ?item ?itemLabel | {{SPARQL2|query=SELECT ?item ?manufacturerLabel ?itemLabel | ||
WHERE | |||
{ | |||
?item scdbt:P1 scdb:Q842 . | |||
?item scdbt:P23 ?manufacturer . | |||
SERVICE wikibase:label { bd:serviceParam wikibase:language "[AUTO_LANGUAGE],en". } | |||
} | |||
}} | |||
=== Ships ordered by cargo capacity === | |||
{{SPARQL2|query=SELECT ?item ?itemLabel ?cargoCapacity WHERE | |||
{ | |||
?item scdbt:P1 scdb:Q842; | |||
scdbt:P61 ?cargoCapacity. | |||
FILTER(0 < ?cargoCapacity). | |||
SERVICE wikibase:label { bd:serviceParam wikibase:language "[AUTO_LANGUAGE],en". } | |||
} ORDER BY DESC(?cargoCapacity) | |||
}} | |||
=== List manufacturers with manufacturer type === | |||
{{SPARQL2|query=SELECT ?item ?itemLabel (GROUP_CONCAT(DISTINCT(?instanceLabel); separator=", ") as ?instances) WHERE { | |||
?item scdbt:P1/scdbt:P26* scdb:Q843; | |||
scdbt:P1 ?instance . | |||
SERVICE wikibase:label { | |||
bd:serviceParam wikibase:language "[AUTO_LANGUAGE],en" . | |||
?item rdfs:label ?itemLabel . | |||
?instance rdfs:label ?instanceLabel | |||
} | |||
} | |||
GROUP BY ?item ?itemLabel | |||
}} | |||
=== Manufacturers with their vehicles (Tree view) === | |||
{{SPARQL2|query=#defaultView:Tree | |||
SELECT ?manufacturer ?manufacturerLabel ?item ?itemLabel | |||
WHERE | WHERE | ||
{ | { | ||
?item scdbt:P1 scdb: | ?item scdbt:P1/scdbt:P26 scdb:Q1181; | ||
scdbt:P23 ?manufacturer. | |||
SERVICE wikibase:label { bd:serviceParam wikibase:language "[AUTO_LANGUAGE],en". } | SERVICE wikibase:label { bd:serviceParam wikibase:language "[AUTO_LANGUAGE],en". } | ||
} ORDER BY ?manufacturerLabel ?itemLabel | |||
}} | |||
=== Search by label === | |||
{{SPARQL2|query=SELECT ?item ?itemLabel WHERE { | |||
?item rdfs:label ?itemLabel. | |||
FILTER(REGEX(?itemLabel, "^Ballista.*", "i")) | |||
} | } | ||
LIMIT 10 | |||
}} | }} | ||
Line 33: | Line 77: | ||
SERVICE wikibase:label { bd:serviceParam wikibase:language "[AUTO_LANGUAGE],en". } | SERVICE wikibase:label { bd:serviceParam wikibase:language "[AUTO_LANGUAGE],en". } | ||
} | } | ||
}} | |||
=== List spaceship build date (Timeline) === | |||
{{SPARQL2|query=#defaultView:Timeline | |||
SELECT ?item ?itemLabel ?inception WHERE | |||
{ | |||
?item scdbt:P1 scdb:Q842; | |||
scdbt:P2 ?inception. | |||
SERVICE wikibase:label { bd:serviceParam wikibase:language "[AUTO_LANGUAGE],en". } | |||
} ORDER BY DESC(?cargoCapacity) | |||
}} | |||
=== Ship vendors with ship list === | |||
{{SPARQL2|query=#defaultView:Tree | |||
SELECT ?vendor ?vendorLabel ?item ?itemLabel | |||
WHERE | |||
{ | |||
?item scdbt:P1/scdbt:P26 scdb:Q1181; | |||
scdbt:P83 ?vendor . | |||
SERVICE wikibase:label { bd:serviceParam wikibase:language "[AUTO_LANGUAGE],en". } | |||
} | |||
}} | |||
=== All items sold with vendor and price === | |||
{{SPARQL2|query=#defaultView:Tree | |||
SELECT ?item ?itemLabel ?vendor (concat(?vendorLabel," - ",STR(?price)," aUEC") AS ?concat) | |||
WHERE | |||
{ | |||
?item scdbp:P83 ?statement . | |||
?statement scdbps:P83 ?vendor . | |||
?statement scdbpq:P87 ?price . | |||
SERVICE wikibase:label { | |||
bd:serviceParam wikibase:language "[AUTO_LANGUAGE],en". | |||
?vendor rdfs:label ?vendorLabel. | |||
?item rdfs:label ?itemLabel. | |||
} | |||
} ORDER BY ?itemLabel ?price | |||
}} | |||
=== All statements poiting to Aegis Dynamics (Q2) === | |||
{{SPARQL2|query=SELECT ?prop ?propLabel ?item ?itemLabel ?itemDescription | |||
WITH | |||
{ | |||
SELECT * | |||
WHERE | |||
{ | |||
?item ?wdt scdb:Q2 . | |||
?prop wikibase:directClaim ?wdt . | |||
} | |||
} as %test | |||
WHERE | |||
{ | |||
hint:Query hint:optimizer "None". | |||
INCLUDE %test | |||
SERVICE wikibase:label { bd:serviceParam wikibase:language "[AUTO_LANGUAGE],en". } | |||
} | |||
}} | |||
== Maintenance Queries == | |||
=== List everything from Query Service === | |||
{{SPARQL2|query=SELECT ?item ?itemLabel | |||
WHERE { | |||
?item rdfs:label ?itemLabel. | |||
} ORDER BY ?item | |||
}} | |||
=== Find items with same scunpacked reference === | |||
{{SPARQL2|query=SELECT ?id (COUNT(?obj) AS ?count) (GROUP_CONCAT(?obj; SEPARATOR = " , ") AS ?items) | |||
WHERE | |||
{ | |||
?obj scdbt:P78 ?id. | |||
} | |||
GROUP BY ?id | |||
HAVING(?count > 1) | |||
}} | }} |
Latest revision as of 20:17, 24 March 2021
Simple Queries
These basic queries help to understand SPARQL and the Wikibase RDF format.
Ships with manufacturer
SELECT ?item ?manufacturerLabel ?itemLabel
WHERE
{
?item scdbt:P1 scdb:Q842 .
?item scdbt:P23 ?manufacturer .
SERVICE wikibase:label { bd:serviceParam wikibase:language "[AUTO_LANGUAGE],en". }
}
Ships ordered by cargo capacity
SELECT ?item ?itemLabel ?cargoCapacity WHERE
{
?item scdbt:P1 scdb:Q842;
scdbt:P61 ?cargoCapacity.
FILTER(0 < ?cargoCapacity).
SERVICE wikibase:label { bd:serviceParam wikibase:language "[AUTO_LANGUAGE],en". }
} ORDER BY DESC(?cargoCapacity)
List manufacturers with manufacturer type
SELECT ?item ?itemLabel (GROUP_CONCAT(DISTINCT(?instanceLabel); separator=", ") as ?instances) WHERE {
?item scdbt:P1/scdbt:P26* scdb:Q843;
scdbt:P1 ?instance .
SERVICE wikibase:label {
bd:serviceParam wikibase:language "[AUTO_LANGUAGE],en" .
?item rdfs:label ?itemLabel .
?instance rdfs:label ?instanceLabel
}
}
GROUP BY ?item ?itemLabel
Manufacturers with their vehicles (Tree view)
#defaultView:Tree
SELECT ?manufacturer ?manufacturerLabel ?item ?itemLabel
WHERE
{
?item scdbt:P1/scdbt:P26 scdb:Q1181;
scdbt:P23 ?manufacturer.
SERVICE wikibase:label { bd:serviceParam wikibase:language "[AUTO_LANGUAGE],en". }
} ORDER BY ?manufacturerLabel ?itemLabel
Search by label
SELECT ?item ?itemLabel WHERE {
?item rdfs:label ?itemLabel.
FILTER(REGEX(?itemLabel, "^Ballista.*", "i"))
}
LIMIT 10
List of systems
SELECT ?item ?itemLabel WHERE {
?item scdbt:P1 scdb:Q16.
SERVICE wikibase:label { bd:serviceParam wikibase:language "[AUTO_LANGUAGE],en". }
}
List of jump points of Stanton system
SELECT ?item ?itemLabel WHERE {
?item scdbt:P1 scdb:Q916;
scdbt:P48 scdb:Q24.
SERVICE wikibase:label { bd:serviceParam wikibase:language "[AUTO_LANGUAGE],en". }
}
STAR arkmap type
SELECT ?item ?itemLabel
WHERE
{
?item scdbt:P10 "STAR".
SERVICE wikibase:label { bd:serviceParam wikibase:language "[AUTO_LANGUAGE],en". }
}
List spaceship build date (Timeline)
#defaultView:Timeline
SELECT ?item ?itemLabel ?inception WHERE
{
?item scdbt:P1 scdb:Q842;
scdbt:P2 ?inception.
SERVICE wikibase:label { bd:serviceParam wikibase:language "[AUTO_LANGUAGE],en". }
} ORDER BY DESC(?cargoCapacity)
Ship vendors with ship list
#defaultView:Tree
SELECT ?vendor ?vendorLabel ?item ?itemLabel
WHERE
{
?item scdbt:P1/scdbt:P26 scdb:Q1181;
scdbt:P83 ?vendor .
SERVICE wikibase:label { bd:serviceParam wikibase:language "[AUTO_LANGUAGE],en". }
}
All items sold with vendor and price
#defaultView:Tree
SELECT ?item ?itemLabel ?vendor (concat(?vendorLabel," - ",STR(?price)," aUEC") AS ?concat)
WHERE
{
?item scdbp:P83 ?statement .
?statement scdbps:P83 ?vendor .
?statement scdbpq:P87 ?price .
SERVICE wikibase:label {
bd:serviceParam wikibase:language "[AUTO_LANGUAGE],en".
?vendor rdfs:label ?vendorLabel.
?item rdfs:label ?itemLabel.
}
} ORDER BY ?itemLabel ?price
All statements poiting to Aegis Dynamics (Q2)
SELECT ?prop ?propLabel ?item ?itemLabel ?itemDescription
WITH
{
SELECT *
WHERE
{
?item ?wdt scdb:Q2 .
?prop wikibase:directClaim ?wdt .
}
} as %test
WHERE
{
hint:Query hint:optimizer "None".
INCLUDE %test
SERVICE wikibase:label { bd:serviceParam wikibase:language "[AUTO_LANGUAGE],en". }
}
Maintenance Queries
List everything from Query Service
SELECT ?item ?itemLabel
WHERE {
?item rdfs:label ?itemLabel.
} ORDER BY ?item
Find items with same scunpacked reference
SELECT ?id (COUNT(?obj) AS ?count) (GROUP_CONCAT(?obj; SEPARATOR = " , ") AS ?items)
WHERE
{
?obj scdbt:P78 ?id.
}
GROUP BY ?id
HAVING(?count > 1)