SQL Pivot Table with Dynamic Columns
Filed under BlackBerry , SQL
Last week, I was tasked with sending out an email of hourly production data that was to be read on blackberry phones. It started off as something really simple; just sending a plain html table in an email.
Something like this:

Moreover, this turned into something much more complex when I found out that I needed to create a pivot chart which displayed the hourly times as dynamic columns. I had never been asked to do anything like this before, and It took me a little bit out of my comfort zone. However, after finding a ton information on this topic, this task became relativily easy.
How to create a pivot chart with dynamic columns?
<cfquery name="qryPivot" datasource="#nscdb#">
DECLARE @columns VARCHAR(8000)
SELECT @columns = COALESCE(@columns + ',[' + cast(Date as varchar) + ']',
'[' + cast(Date as varchar)+ ']')
FROM vGroupMail
GROUP BY Date
DECLARE @query VARCHAR(8000)
SET @query = '
SELECT *
FROM vGroupMail
PIVOT
(
MAX(Pac)
FOR [Date]
IN (' + @columns + ')
)
AS p'
EXECUTE(@query)
</cfquery>
And thats it!!! Once you study this code or view someone else's post on the topic, you shouldn't have a problem.
http://www.simple-talk.com/community/blogs/andras/archive/2007/09/14/37265.aspx
http://blog.crowe.co.nz/archive/2005/09/05/250.aspx
Here is the output in an html table.

*******************************************
| View count: 3192Feb24








