Sometimes when we run SQL queries we might want to group the results by a given column. A common way to do this is with the GROUP BY
clause.
But sometimes the GROUP BY
clause isn’t enough.
Sometimes we want the results presented in a way that the GROUP BY
clause doesn’t cater for. The GROUP BY
is perfect if we want to collapse groups of rows into a single row, so that we can get an aggregate value for each set of collapsed rows. But sometimes we don’t want this. Sometimes we want to see all of the rows, as well as the aggregate values.
This is where the PARTITION BY
clause comes in.