1

Consdier I have the following RDF data

:car1 :hasColor :red.
:car1 :hasColor :blue.

:car2 :hasColor :blue.
:car2 :hasColor :red.

I want to count cars by their colors sets, i.e., that would result a single group {:blue, :red} and two cars within the group {:car1, :car2} and the count will be 2. The following query looks like the intuitive way to perform the required. However, it is not guaranteed to always work because GROUP_CONCAT removes any order before concatenating strings (See another question here). This means: I may get ":red :blue" for first car, and : ":blue :red" for second car, so that they will not be considered in the same group.

Is there any way to achieve what I want?

SELECT  (COUNT(?car) AS ?count) ?colorGroup
WHERE
  { { SELECT  ?car (GROUP_CONCAT(?color) AS ?colorGroup)
      WHERE
        { { SELECT  ?car ?color
            WHERE
              { ?car  :hasColor  ?color }
            ORDER BY ?color
          }
        }
      GROUP BY ?car
    }
  }
GROUP BY ?colorGroup
Median Hilal
  • 1,483
  • 9
  • 17
  • use `ORDER BY ?car ?color` in the inner subquery instead of just `ORDER BY ?color` – Damyan Ognyanov Sep 30 '19 at 08:21
  • @DamyanOgnyanov how would this help? `GROUP_CONCAT` uses multiset semantics - no order guaranteed, everything implementation specific. One triple store does it, the other triple store doesn't – UninformedUser Sep 30 '19 at 12:51
  • This is being considered as part of the [SPARQL 1.2 effort](https://github.com/w3c/sparql-12/issues/9). – TallTed Oct 04 '19 at 19:32

0 Answers0