A lot of these tricks are fairly standard in the world of column-oriented databases. I did many calculations like these with kdb+ (http://kx.com) when I worked in finance.
If there is a demand for tricks like these, I'd be happy to write about them (using Postgres as the underlying database of course).