Complex sql query (for me anyway)

See my last reply about dynamic column names sent as parameters. Also more on this topic: