Topic: Reference a Dynamic Column when outputting

jaco5md    -- 04-14-2011 @ 3:04 PM
  I have a somewhat unique issue I'm not sure how to deal with.  I am using a pivot table within my query to get data ready to display, however, two of the column names get generated dynamically.  How can I reference them when I output the data from teh query without knowing the column names first?  I hope I'm not being realy stupid but I can't seem to figure this out and was hoping you could point me in the right direction.

I'm doing this so I don't have to keep changing the report from year-to-year, making it more dynamic.

Here is what I am using:

<cfquery name="getP" datasource="#request.DSN#">
Select Top 2 ID, Left(Program,4) AS ProgYear
FROM vaccine_program
WHERE Program like '%Seasonal Flu%'
Order By ID Desc

<cfset program = ArrayNew(2)>

<cfloop query="getP">
<cfset program[currentrow][1] = #getP.ID#>
<cfset program[currentrow][2] = #getP.ProgYear#>

Using "Select *" so I can pivot the data.

<cfquery name="getVariance" datasource="#request.DSN#">
FROM    (SELECT * FROM vw_Variance) AS DataTable
(SUM (RequestTotal) FOR ProgramID IN ([#program[2][1]#],[#program[1][1]#])) AS PivotTable

Currently [#program[2][1]#] = 3 and [#program[1][1]#] = 1 however next year the values may be 3 and 5.

I hope this makes sense.

I've tried using this:
<cfset colList = getVariance.ColumnList>
and concatenating the variable but that didn't work, I'm not sure what else to try.  Thanks for your time and attention.



"Rock 'n' Roll 4 Ever"

Lyndon    -- 04-14-2011 @ 4:04 PM
  Select *  is, almost, never a good solution. It is hard to help and/or make sense of what you are trying to accomplish with out knowing the Table column names.
Show a few of the column names in table: vw_Variance and an example of the output you are trying to produce and maybe there will be enough information to help you.


jaco5md    -- 04-14-2011 @ 4:08 PM
  Thanks Lyndon,

I usually don't use Select *, but since two of these tablenames are going to be dynamic based on the array variables I thought I had to in order to successfully do the pivot.

Good news is I just figured out how to accomplish what I needed.  Will post solution soon.


"Rock 'n' Roll 4 Ever"

EasyCFM.COM ColdFusion Forums :