Group Concat using sparql query - sparql

Can some one help me how to use group_concat in sparql query. When i am using in the database it is pulling out iri from the data. How to pull the the labels for the objects present in the database using group_concat.

You will usually have IRIs with a label. Now, to return labels only, a query like this will work:
SELECT ?item ?label
WHERE {
?item rdfs:label ?label
}
Note that rdfs:label is conventionally used for labels, but any name can be used - it depends on the data that you have.
Now, imagine we want to find a GROUP_CONCAT of children's names, grouped by mother.
First, the query for the children and labels looks like this:
SELECT ?mother ?child ?label
WHERE {
?mother :hasChild ?child .
?child rdfs:label ?label .
}
Notice that ?mother and ?child will be bound to IRIs here, and ?label to a string.
Now for the GROUP_CONCAT, you will just need a query like:
SELECT ?mother (GROUP_CONCAT(?label; SEPARATOR=", ") AS ?concat)
WHERE {
?mother :hasChild ?child .
?child rdfs:label ?label .
}
GROUP BY ?mother

Related

SPARQL query - multiple OPTIONALS

I want to retrieve the entities of the classes along with some information - if exists (e.g. label, comment, image)
I run the following query:
PREFIX schema: <http://schema.org/>
SELECT DISTINCT ?entity ?label ?comment ?image
WHERE {
?entity a ?class .
OPTIONAL {?entity rdfs:label ?label } .
OPTIONAL {?entity schema:image ?image} .
OPTIONAL {?entity rdfs:comment ?comment} .
OPTIONAL {?entity rdfs:label ?label} .
FILTER (langMatches(lang(?comment), "EN") && langMatches(lang(?label), "EN")) .
}
Although, some triples contain this extra information I do not receive them
(even if I re-write and include all the optional statements in one statement)
How could I compose such a query?

i want to get the names of similar types using sparql queries from dbpedia

I need to find the names of similar types from DBpedia so I'm trying to figure out a query which can return me the names of entities which have same subject type in its dct:subject (example I want to find similar types of white house so i want to write a query for same . I'm considering the dct:subject to find them ). If there is any other approach please mention it
Previously I tried it for rdf:type but the result are not so good and some time it shows time out
I have done my problem by the query mentioned below and now i want to consider dct:subject instead of rdf:type
select distinct ?label ?resource count(distinct ?type) as ?score where {
values ?type { dbo:Thing dbo:Organization yago:WikicatIslam-relatedControversies yago:WikicatIslamistGroups yago:WikicatRussianFederalSecurityServiceDesignatedTerroristOrganizations yago:Abstraction100002137 yago:Act100030358 yago:Cabal108241798 yago:Group100031264 yago:Movement108464601 yago:PoliticalMovement108472335
}
?resource rdfs:label ?label ;
foaf:name ?name ;
a ?type .
FILTER (lang(?label) = 'en').
}
ORDER BY DESC(?score)

How to express equals relation in sparql?

I am trying to use sparql to find all French labels of words that are related to a given English word.
The query I made is something like the following:
SELECT ?extword ?word ?label where {
?word rdfs:label "shark"#eng.
{?extword rdf:type ?word}
UNION
{?word rdf:type ?extword}.
?extword rdfs:label ?label filter(lang(?label)="fra")
}
While it does give me the French labels of the extword as desired, it does not give the French labels of word itself.
What I need is to insert a clause to express the relation that extword is exactly word, like this:
SELECT ?extword ?word ?label where {
?word rdfs:label "shark"#eng.
{?extword = ?word}
UNION
{?extword rdf:type ?word}
UNION
{?word rdf:type ?extword}.
?extword rdfs:label ?label filter(lang(?label)="fra")
}
It turns out to be syntax error. Is it possible to also include the French labels of word itself, without doing another query?
To be clearer, the result I expect is the result from my first query combined with result from the following query:
SELECT ?word ?label where {
?word rdfs:label "shark"#eng.
?word rdfs:label ?label filter(lang(?label)="fra")
}

SPARQL for getting movies list with proper attributes ordered

I'm trying to get list of movies in a specific year (just say 1990) with each movie and it's attributes. I tried different ways to get SPARQL but couldn't. How do I group genres into single column?
The format I would want is with some of the properties from here
http://schema.org/Movie
PREFIX wd: <http://www.wikidata.org/entity/>
PREFIX wdt: <http://www.wikidata.org/prop/direct/>
PREFIX wikibase: <http://wikiba.se/ontology#>
PREFIX p: <http://www.wikidata.org/prop/>
PREFIX v: <http://www.wikidata.org/prop/statement/>
PREFIX q: <http://www.wikidata.org/prop/qualifier/>
PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#>
SELECT ?q ?film_title (GROUP_CONCAT(DISTINCT(?genreID); separator=", ") as ?genre) WHERE {
?q wdt:P31 wd:Q11424.
?q rdfs:label ?film_title filter (lang(?film_title) = "en").
?q wdt:P136 ?genreID.
?genreID rdfs:label ?genre.
?q wdt:P161 ?actorID.
?actorID rdfs:label ?actor filter (lang(?actor) = "en").
} LIMIT 100
Usage of aggregate functions usually need to build the group of things/rows on which you want to apply the function, i.e. a GROUP_BY on the projection variables that do not denote the result of an aggregate function:
SELECT ?q ?film_title (GROUP_CONCAT(DISTINCT(?genreID); separator=", ") as ?genres) WHERE {
?q wdt:P31 wd:Q11424 .
?q rdfs:label ?film_title .
FILTER (lang(?film_title) = "en")
?q wdt:P136 ?genreID .
?genreID rdfs:label ?genre .
?q wdt:P161 ?actorID .
?actorID rdfs:label ?actor
FILTER (lang(?actor) = "en")
}
GROUP BY ?q ?film_title
LIMIT 100
Some comments:
it's not clear whether you want to get the genreID or the label of the genre, for the latter indeed you have to use the variable ?genre in the GROUP_CONCAT function: GROUP_CONCAT(DISTINCT(?genre); separator=", ")
it's not allowed to re-assign a variable name that has already been used in the query, that's why I changed the name to ?genres
don't put a comma after a FILTER, I don't know why you did so
you don't select the actor, thus, it remains unclear why you use the last triple pattern + FILTER in your query
your query is quite expensive, thus, its execution might timeout

how to extract URI with sparql query

I need to extract the URIs of a specific resource from DBpedia.
Example, my specific resource is "France", I extracted its label but I don't know how to get its URI.
Here is my query so far :
SELECT ?label
WHERE{
res:France rdfs:label ?label .
FILTER (lang(?label) = 'fr') .}
Thanks for your help !
You need to filter your resource to be able to extract the URI. Try this:
SELECT distinct *
WHERE{
?uri rdfs:label ?label
filter (?uri=res:France && lang(?label) = 'fr')
}