Sparql: how to GROUP BY More Than One Column

2019-06-15 04:01发布

问题:

In SPARQL, we can group the rows by a column through the gollowing syntax:

GROUP BY ?colName

Can we group by more than 1 columns eg:

GROUP BY (?colName1 + ?colName2 + ?colName3) 

Suppose a query like:

Select ?a ?b ?c (MIN(?y) AS ?d)
Where {
....
}
GROUP BY (?a + ?b + ?c)

But this query does not work.

回答1:

You can GROUP BY multiple variables (not columns) by listing them with a space in between:

GROUP BY ?a ?b ?c


回答2:

In addition to Ben Companjen's answer of

GROUP BY ?a ?b ?c

you need to fix the SELECT line as you can't pass out the indeterminate non-group keys without explicitly saying so e.g.

SELECT (sample(?a) as ?A) (sample(?b) as ?B) (sample(?c) as ?C) (min(?y) as ?d)


回答3:

Have you tried something like

SELECT ?a+b+?c, (MIN(?y) AS ?d) Where { .... } GROUP BY (?a+?b+?c)

This works in SQL Server just fine