Re: getting multivalued properties in a single query

On 6/28/2010 8:12 AM, Sergio Fernández wrote:
> Hi,
>
> I wonder if anyone knows how can I write a single SPARQL query to
> retrieve all triples with the same triple pattern without post-process
> the query result.
>
> Let's put an example: using FOAF is quite common to have a dataset like:
>
> #person1 foaf:name "Person1" .
> #person1 foaf:knows #person2 .
> #person1 foaf:knows #person3 .
> ...
> #person1 foaf:knows #personN .
>
> How can I get all knows for a concrete subject? And I'd like to do it
> just with SPARQL in a single query. So the two ways that I know:
>
> 1. Post-process the query's result, merging result about the same subject
> 2. Perform two queries: one for the single properties, and a second
> for the multivalued stuff.
>
> are not valid at all. Anyone has any another idea...?
>
> Thanks in advance.
>
> Kind regards,

Hi Sergio,

I think you are saying that you require a single row per subject, but 
want all of the foaf:knows for that subject within that single solution. 
You can't do this in SPARQL 1.0 without post-processing. You will be 
able to do it in SPARQL 1.1 with the GROUP_CONCAT aggregate:

SELECT ?person (GROUP_CONCAT(?knows) AS ?knows_string)
WHERE
{
   ?person foaf:knows ?knows
}
GROUP BY ?person

Lee

Received on Monday, 28 June 2010 13:24:17 UTC