I'm not sure what you are going for since you keep adding additional
criteria/constraints to your problem. At this point you are faced with a
series of trade-offs between caching, speed, and flexibilty, complexity. I
would suggest you break up your requirements into smaller pieces and not go
looking for some kind of magic bullet that will solve all your problems in a
single query. It likely does not exist.
I would also suggest that you look into resources on data warehousing and
the star schema; doing what you are trying directly within the OLTP is
probably not the best solution - especially not on front-end servers. My
experience in this area is thin but maybe someone else can make some
suggestions and/or provide some useful resource links.
David J.
Probably you are right. It seems, that there is no such single-query solution, just only with PostgreSQL. I will break it up into smaller pieces. There is medium-tern plan for data-warehousing, just this one task I will solve yet without it. Maybe this is the way to go then: http://www.jaspersoft.com/tour.
Thanks.
Jano