GROUP BY
For most of the data aggregation purposes, groupby will be the primary - maybe even the only - method to use.
GROUP BY performs aggregation functions by one or more dimension and will perform the
arithmetic by the said dimensions.
Let's say we want to know the average expected profit by product category, we can implement the following query
to get these results
select category, sum((price - cost)*stock_quantity) as expected_profit
from products
group by 1;
Let's look at another example. Suppose we are interested in the average expected_profit per category of product.
Using the same data, we can use the following query
select category,
round(avg((price - cost)*stock_quantity),2) as avg_profit
from products
group by 1;
This concludes our discussion on GROUP BY aggregation. In the next section, we look at HAVING, a post-aggregation filtering technique.