Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Sparql: how to GROUP BY More Than One Column

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.

like image 928
sapthrishi007 Avatar asked Jan 23 '13 10:01

sapthrishi007


People also ask

How to group by with multiple columns in SQL?

Remember this order: SELECT (is used to select data from a database) FROM (clause is used to list the tables) WHERE (clause is used to filter records) GROUP BY (clause can be used in a SELECT statement to collect data across multiple records and group the results by one or more columns)

How to group all columns in SQL?

To arrange similar (identical) data into groups, we use SQL GROUP BY clause. The SQL GROUP BY clause is used along with some aggregate functions to group columns that have the same values in different rows. We generally use the GROUP BY clause with the SELECT statement, WHERE clause, and ORDER BY clauses.


2 Answers

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

GROUP BY ?a ?b ?c
like image 97
Ben Companjen Avatar answered Sep 20 '22 15:09

Ben Companjen


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)
like image 39
AndyS Avatar answered Sep 21 '22 15:09

AndyS