Concatenate one column over a resultset like kind of group_concat - sparql

I am currently writing on a SPARQL query (GraphDB) and try to concatenate the results of one column to avoid the kind of "duplicates".
My current query looks like this
SELECT ?label ?otaupdater
WHERE {
?charger rdf:type mymodel:SoftwarePackage.
?charger mymodel:isCompatibleWith ?target.
?target car:CarId <93718293>.
?charger rdfs:label ?label.
?otaupdater rdf:type mymodel:OTAUpdater.
?charger mymodel:isCompatibleWith ?otaupdater.
}
which gives me a resultset like
label
otaupdater
Chargie_One
123
Chargie_One
456
Chargie_Two
123
I tried to work in the command group_concat but only got so far that I ended up with
SELECT (group_concat(?otaupdater; separator=", ") AS ?otaupdaters) where { ... }
but this only returns all otaupdaters in one builded list.
What I am trying to achieve is something like this:
label
otaupdaters
Chargie_One
123, 456
Chargie_Two
123

The comment of #UninformedUser
lead to the answer:
SELECT ?charger ?label ?target (group_concat(?otaupdater; separator=", ") AS ?otaupdaters)
WHERE {
?charger rdf:type mymodel:SoftwarePackage.
?charger mymodel:isCompatibleWith ?target.
?target car:CarId <93718293>.
?charger rdfs:label ?label.
?otaupdater rdf:type mymodel:OTAUpdater.
?charger mymodel:isCompatibleWith ?otaupdater.
}
GROUP BY ?charger ?label ?target

Related

Query in Sparql to get data of a person

I would like to have a list of all mountains names of the "dbo:firstAscentPerson" property of Lionel Terray (dbpedia: http://dbpedia.org/page/Lionel_Terray).
This below is what I have so far
select ?person ?firstAscentPerson where {
?person foaf:name "Lionel Terray"#en.
dbo:firstAscentPerson rdfs:label ?firstAscentPerson.
} LIMIT 100
The following query does what you need:
SELECT ?mountain ?mountainName ?person2 ?person2Name {
VALUES ?person1Name {"Lionel Terray"#en}
?mountain dbo:firstAscentPerson ?person1, ?person2 .
?mountain rdfs:label ?mountainName .
?person1 foaf:name ?person1Name .
FILTER (lang(?mountainName) = "en")
OPTIONAL {?person2 foaf:name ?person2Name . FILTER (lang(?person2Name) = 'en')}
}

transaction time out in sparql query

select distinct ?label ?resource count(distinct ?type) as ?score
where
{
values ?type
{ <http://dbpedia.org/class/yago/Abstraction100002137>
<http://dbpedia.org/class/yago/Company108058098>
<http://dbpedia.org/class/yago/ElectronicsCompany108003035>
<http://dbpedia.org/class/yago/Group100031264>
<http://dbpedia.org/class/yago/Institution108053576>
<http://dbpedia.org/class/yago/Organization108008335>
}
?resource rdfs:label ?label ;
foaf:name ?name ;
a ?type .
FILTER (lang(?label) = 'en').
}
ORDER BY DESC(?score)
limit 10
I am trying to run this query but it shows transaction timeout, I cut out many of these links from this query, but still it shows same issue

Free text search in sparql when you have multiword and scaping character

I am wondering how I can use in sparql query when I have a word like :
Robert J. O'Neill
I am looking for the resource that have the multiword unit with quota or unicode character in the Label property.
SELECT DISTINCT ?resource ?abstract
WHERE {?resource rdfs:label ?s.
?s <bif:contains> "'Robert J. O'Neill'"
?resource dbo:abstract ?abstract
}
'''
Here is the query that will return all the elements that have "Robert J. O'Neill" as label.
SELECT DISTINCT ?s WHERE
{
?s rdfs:label ?label .
FILTER(regex(?label, "Robert J. O'Neill", "i"))
}
If you are sure that you need a specific string matching. This is faster :
SELECT DISTINCT ?s WHERE
{
?s rdfs:label ?label .
?label bif:contains "Robert J. O'Neill"
}
But be aware that, Virtuoso for example doesnt support such a query because of the spaces in the string. So an alternative is to avoid it as :
SELECT DISTINCT * WHERE
{
?s rdfs:label ?label .
?label bif:contains "Robert" .
FILTER (CONTAINS(?label, " J. O'Neill"))
}
I found following code faster that the regex:
SELECT ?s WHERE { ?s rdfs:label ?o FILTER ( bif:contains ( ?o, '"Robert" AND "J." AND "Neill"' ) ) }

How the pass the output of one sparql query as a input to another sparql query

I am trying get the dbpedia movie link using the movie name in the first query and pass that link in the second query to get the movies similar to this movie.For e.g Lagaan.Now instead of passing the link manually in the second query is there a way to combine the two queries and pass the output of first query as an input to the second query.i.e:the link of the movie lagaan.Also,if the first query gives multiple links eg:if i am searching for Harry potter it will return multiple harry potter series links so,it should handle that case as well.
Query1
prefix rdfs: <http://www.w3.org/2000/01/rdf-schema#>
prefix dbpedia-owl: <http://dbpedia.org/ontology/>
select distinct ?film where {
?film a dbpedia-owl:Film .
?film rdfs:label ?label .
filter regex( str(?label), "Lagaan", "i")
}
limit 10
Query 2
PREFIX dbpedia-owl: <http://dbpedia.org/ontology/>
select ?similar (count(?p) as ?similarity) where {
values ?movie { <http://dbpedia.org/resource/Lagaan> }
?similar ?p ?o ; a dbpedia-owl:Film .
?movie ?p ?o .
}
group by ?similar ?movie
having count(?p) > 35
order by desc(?similarity)
Edited query:
select ?film ?similar (count(?p) as ?similarity) where {
{
select distinct ?film where {
?film a dbpedia-owl:Film .
?film rdfs:label ?label .
filter regex( str(?label), "Lagaan", "i")
}
}
?similar ?p ?o ; a dbpedia-owl:Film .
?film ?p ?o .
}
group by ?similar ?film
having count(?p) > 35
order by desc(?similarity)
corrected query as told by Joshua Taylor
select ?film ?other (count(*) as ?similarity) {
{
select ?film where {
?film a dbpedia-owl:Film ; rdfs:label ?label .
filter contains(lcase(?label),"lagaan")
}
limit 1
}
?film ?p ?o .
?other a dbpedia-owl:Film ; ?p ?o .
}
group by ?film ?other
having count(?p) > 25
order by desc(?similarity)
is there a way to combine the two queries and pass the output of first
query as an input to the second query.
SPARQL 1.1 defines subqueries. The results of inner queries are available to outer queries, so they are "passed" to them. In your case, you would have something along the lines of:
select ?similarMovie (... as ?similarity) where {
{ #-- QUERY 1, find one or more films
select distinct ?film where {
#-- ...
}
}
#-- QUERY 2, find films similar to ?film
#-- ...
}

SPARQL entity lists inside select queries

In the following DBpedia query, is there a way to consolidate the UNIONs into a single pattern?
PREFIX prop: <http://resedia.org/ontology/>
PREFIX res: <http://resedia.org/resource/>
SELECT DISTINCT ?language ?label
WHERE {
{res:Spain prop:language ?language}
UNION
{res:France prop:language ?language}
UNION
{res:Italy prop:language ?language}
?language rdfs:label ?label .
FILTER langMatches(lang(?label), "en")
}
The SPARQL spec mentions something about RDF collections but I don't really understand what it's describing. It seemed like the following syntax should work, but it didn't.
PREFIX prop: <http://resedia.org/ontology/>
PREFIX res: <http://resedia.org/resource/>
SELECT DISTINCT ?language ?label
WHERE {
(res:Spain res:France res:Italy) prop:language ?language
?language rdfs:label ?label .
FILTER langMatches(lang(?label), "en")
}
Is there a way to define a list (or "multiset", or "bag") of URIs like this inside a SELECT query?
In SPARQL 1.1 you can do
SELECT DISTINCT ?language ?label
WHERE {
?country prop:language ?language .
?language rdfs:label ?label .
VALUES ?country { res:Spain res:France res:Italy }
FILTER langMatches(lang(?label), "en")
}
Simple answer: no.
(res:Spain res:France res:Italy) prop:language ?language
means 'match where a list containing Spain, France and Italy has a language', i.e. the list itself has a language.
You could do:
?country prop:language ?language . ?language rdfs:label ?label .
FILTER ( ?country == res:Spain || ?country == res:France || ?country == res:Italy )
which is shorter, but may be slower.
(I had a feeling SPARQL 1.1 had an 'IN' feature, but I don't see it in the drafts)