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