You might also consider placing your pivot code in the database in the form of a stored procedure. This centralizes your pivot logic and opens up additional options (i.e. google on pivot table for Oracle PL/SQL or SQL server s-procs, etc. and you will find ample examples).