Hello all.

I would like to ask about the semantics that the GROUP_CONCAT function comes with.

I experimented a little bit with Jena and if I use UNIONs and GROUP_CONCAT then I am getting an error. I know that UNION would also give for multiple variables a lot of null values. I thought that GROUP_CONCAT would have to problem to take them and do something like: (null, null, dbpedia:Peter). Can't UNION and G_C exist and work together? I always have to use joins (.) to connect triple patterns?

Example is:

SELECT 
    (GROUP_CONCAT(DISTINCT ?variable1; separator = ',') as ?var1)
    (GROUP_CONCAT(DISTINCT ?variable2; separator = ',') as ?var2)
WHERE { 
   { <http://dbpedia.org/resource/Peter> some_predicate_1 ?variable1 } UNION 
   { ?variable2 some_predicate_2 <http://dbpedia.org/resource/John> }
}

An alternative of course whould be to join the triple patterns. However, the problem using joins of triple patterns is that if a variable returns nothing as result, even if the variables are not practically joined with each other since they have nothing in common, then the whole query returns a null value as a result.

P.S.: One would suggest to use OPTIONAL. I already asked a question about OPTIONAL and I don't want to use it for some reasons. I want to use a set of UNIONs however together with GROUP_CONCAT since this function is very helpful for me.

asked 10 Jan '13, 13:10

Christos's gravatar image

Christos
15417
accept rate: 33%

edited 10 Jan '13, 13:12


UNION is do each branch separately. So some rows have ?variable1 and not ?variable2, the other rows have ?variable2 not ?variable1. Try the query

SELECT * 
WHERE { 
   { <http://dbpedia.org/resource/Peter> some_predicate_1 ?variable1 } UNION 
   { ?variable2 some_predicate_2 <http://dbpedia.org/resource/John> }
}

and look at the rows.

Now when the whole thing gets aggregated (you have an aggregation in the SELECT and no GROUP BY), one of the other is an error.

Try pushing the GROUP_CONCAT into the UNION:

SELECT *
WHERE { 
   { SELECT (GROUP_CONCAT(DISTINCT ?variable1; separator = ',') as ?var1) 
     { <http://dbpedia.org/resource/Peter> some_predicate_1 ?variable1 } }
   } UNION {
   { SELECT (GROUP_CONCAT(DISTINCT ?variable2; separator = ',') as ?var2)
     {?variable2 some_predicate_2 <http://dbpedia.org/resource/John> }
   }
}
permanent link

answered 10 Jan '13, 16:16

AndyS's gravatar image

AndyS ♦
13.4k37
accept rate: 33%

Thats a very good idea!

Thanks I will check it tonight and see what results I get. I have one question though, the * will make the query do the computation also for the variables that are not inside the GROUP_CONCAT. Isn't this an overhead since I don't care about those. I have a feeling that if I specify what variables I want, I will cause a Jena error

(10 Jan '13, 16:59) Christos Christos's gravatar image

Unused variables are free in Jena.

but anyway the only variable coming out of

SELECT (GROUP_CONCAT(DISTINCT ?variable2; separator = ',') as ?var2)

is ?var2. Aggregation will hide the ?variable2 aand anything else inside the group (it's a group of the whole pattern in this case - no GROUP BY)

(11 Jan '13, 04:07) AndyS ♦ AndyS's gravatar image
Your answer
toggle preview

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Markdown Basics

  • *italic* or _italic_
  • **bold** or __bold__
  • link:[text](http://url.com/ "title")
  • image?![alt text](/path/img.jpg "title")
  • numbered list: 1. Foo 2. Bar
  • to add a line break simply add two spaces to where you would like the new line to be.
  • basic HTML tags are also supported

Question tags:

×1,284
×602

question asked: 10 Jan '13, 13:10

question was seen: 875 times

last updated: 14 Jan '13, 13:25