How to calculate statistical data

You must define a new ModelSQL that you will query.
Here is an example: https://hg.tryton.org/modules/timesheet/file/ee4b51fe0754/line.py#l230