I have a little problem with PostgreSQL, i could do everything with CASE WHEN but I hope there is another method to do this.
In a few words I have to calculate an AVG val