trdsql Window Functions
So far, we have introduced aggregation by grouping, which outputs rows for each group completely separate from the original rows. In other words, it was outputting the results of the aggregation separately from the original file.
However, there may be cases where you want to output the aggregation results in addition to the information in the original file. For example, in a column of scores, you can output the difference from the average score and perform flexible calculations. Until now, the only way to do this was to aggregate first and then JOIN, but with SQL’s window functions, you can also output such aggregations.
In older versions, SQLite3 did not support window functions, but the SQLite3 included in the current trdsql does support them.
Window functions can also be used in PostgreSQL and MySQL, but be aware that MySQL supports them from version 8.0 onwards.
Displaying the Total
The calculation of the total was output in aggregate calculation, but it was only outputting the final result. With window functions, you can display the result for each row.
For example, let’s display the results for the following CSV of scores.
Window functions perform calculations by specifying a range or order by adding an OVER () clause to the function of the aggregation function.
If you specify an empty OVER() clause, all rows will be targeted.
The total is calculated with SUM(), so the average can also be calculated with AVG().
Difference from the average
You can calculate the average using window functions and the target row. When you display the difference, it will look like this.
round() is a function that rounds off the decimal part.
Until now, SQL was limited to processing one row at a time or grouping, but with window functions, you can specify a range of rows and perform calculations for each row.
The range of rows can be specified by columns similar to those specified by “GROUP BY” after “PARTITION BY”.
Let’s output the changes by class instead of the whole.
In class A, the difference from the average of 195 is displayed, and in class B, the difference from the average of 239 is displayed.
You can also display the difference from the overall average by simply listing it.
Assigning Row Numbers
Window functions can also be used to assign row numbers.
fruits.ltsv
The ROW_NUMBER() function is a window function unique to window functions that is not an aggregation function. When used with OVER(), it is used for the entire range without specifying an order, as shown below.
To specify the order, use “ORDER BY”.
There are still many other types of functions in window functions. Please refer to the manual of each database.