我已經包括在頂部放置您的cfquery語句的示例數據行。
<cfset firstQuery = queryNew("date,NumberofPeople,EmploymentRate")>
<cfset aRow = queryAddRow(firstQuery)>
<cfset querySetCell(firstQuery,"date","OCT_2011",aRow)>
<cfset querySetCell(firstQuery,"NumberofPeople","28",aRow)>
<cfset querySetCell(firstQuery,"EmploymentRate","50%",aRow)>
<cfset aRow = queryAddRow(firstQuery)>
<cfset querySetCell(firstQuery,"date","NOV_2011",aRow)>
<cfset querySetCell(firstQuery,"NumberofPeople","28",aRow)>
<cfset querySetCell(firstQuery,"EmploymentRate","56%",aRow)>
<cfset aRow = queryAddRow(firstQuery)>
<cfset querySetCell(firstQuery,"date","DEC_2011",aRow)>
<cfset querySetCell(firstQuery,"NumberofPeople","29",aRow)>
<cfset querySetCell(firstQuery,"EmploymentRate","55%",aRow)>
<cfset aRow = queryAddRow(firstQuery)>
<cfset querySetCell(firstQuery,"date","JAN_2012",aRow)>
<cfset querySetCell(firstQuery,"NumberofPeople","30",aRow)>
<cfset querySetCell(firstQuery,"EmploymentRate","52%",aRow)>
<!--- Will Create new query with names as column headers--->
<cfset newQuery = queryNew(valueList(firstQuery.date,','))>
<!--- Will Create new query with names as column headers--->
<cfset people = queryAddRow(newQuery)>
<cfset rate = queryAddRow(newQuery)>
<cfloop query='firstQuery'>
<!---Syntax for this function is: QuerySetCell(query, column_name, value [, row_number ]) --->
<cfset querySetCell(newQuery,firstQuery.date,firstQuery.NumberofPeople,people)>
<cfset querySetCell(newQuery,firstQuery.date,firstQuery.EmploymentRate,rate)>
</cfloop>
<cfdump var="#newQuery#">
<cfdump var="#ArrayToList(newQuery.getColumnNames())#">
這是我該怎麼做,但我想不出爲什麼我會這樣做。我有興趣聽聽你的用例。無論如何,我希望這有助於。
(PS。這在CF9測試,所以你應該能夠複製並粘貼到自己試一試)
編輯 - (再次):
忘了提,這可只有在從數據庫中檢索的名稱是有效的列名時才起作用,所以沒有空格(在本例中,日期中的空格已被下劃線替代)!
>>>更新數據結構的新代碼片段,函數valueList(firstQuery.date,',')
不會重新排序列。傾銷時,輸出欄會重新排序。我已經使用函數ArrayToList(newQuery.getColumnNames())
來顯示內部CF維護列順序,您只需要很好地問它。你應該能夠使用所有這些信息來很好地輸出你的數據。
您可以更新標籤以指示您的數據庫類型和版本嗎? – Leigh