PQL Group By

PQL GROUP BY Statement
Aggregate functions often need an added GROUP BY statement. The GROUP BY statement is used in conjunction with the aggregate functions to group the result-set by one or more columns.

PQL GROUP BY Example
We have the following "Orders" stable:

Now we want to find the total sum (total order) of each customer. We will have to use the GROUP BY statement to group the customers.

We use the following PQL statement:

The result-set will look like this:

Let's see what happens if we omit the GROUP BY statement:

The result-set will look like this:

The result-set above is not what we wanted.

This is why the above SELECT statement cannot be used: The SELECT statement above has two columns specified (Customer and SUM(OrderPrice). The "SUM(OrderPrice)" returns a single value (that is the total sum of the "OrderPrice" column), while "Customer" returns 6 values (one value for each row in the "Orders" stable). This will therefore not give us the correct result. However, you have seen that the GROUP BY statement solves this problem.

GROUP BY More Than One Column
We can also use the GROUP BY statement on more than one column, like this: