GROUP BY Function | Accessor | Integer [ , Function | Accessor | Integer ]*
Firstly, if a GROUP BY clause is will mean that the List returned by
Query.execute(List)
will contain the columns indicated in the GROUP BY clause.For example:
SELECT lastModified FROM java.io.File GROUP BY name, length
name
and length
rather than the columns rather than the lastModified
value. The reason for this is that JoSQL goes one-step further than conventional SQL and will ALSO return the objects that matched each group by value. These results are stored as a Map
within the Query object and can be retrieved by calling: Query.getGroupByResults()
. The GROUP BY functionality is implemented this way since it is often useful to know which objects matched the key specified by the GROUP BY clause.For example:
SELECT * FROM java.io.File GROUP BY name, length
List res = q.execute (myFiles); Map gbRes = q.getGroupByResults (); for (int i = 0; i < res.size (); i++) { List key = (List) res.get (i); List files = (List) gbRes.get (key); for (int j = 0; j < files.size (); j++) { File f = (File) files.get (j); // Process the File object. } }
For example:
SELECT upper (name), formatDate (lastModified) FROM java.io.File GROUP BY name, length
If you also specify an ORDER BY clause then JoSQL will order the results of each of the matching sets of objects.
For example:
SELECT name, length FROM java.io.File GROUP BY name, length ORDER BY upper (name), length DESC
If you provide a LIMIT clause then JoSQL will apply that to the results, NOT to the group by results. Also, ALL the group by results will still be available in the Query object regardless of whether limiting has occurred on the results.
Care should be taken when using the group by clause however since re-writing the query can sometimes produce better performance, for example:
SELECT @avg_length FROM java.io.File LIMIT 1, 1 EXECUTE ON ALL avg (:_allobjs, length) avg_length
SELECT * FROM java.io.File GROUP BY avg (:_allobjs, length, 'avg_length')
avg
function will produce a different result for each object (which it won't!). In short, case needs to be taken to understand whether you wish grouping of the objects to occur or do you just wish to execute "aggregate" functions on the objects, if you wish to just execute aggregate functions then use "EXECUTE ON", if you wish to actually group the objects according to unique keys use "GROUP BY".