2012-09-11 39 views
1

我有一個簡單的cfquery,它輸出3列及其各自的數據。這些列是名稱,地址和年齡。在ColdFusion中轉置查詢

我想轉置這組數據,以便名稱成爲列,地址和年齡顯示在每列下。

我知道我們可以使用QueryAddColumn或類似的東西來解決這個問題。有人可以幫我解決這個問題嗎?

編輯: 基於以下這個評論是預期輸出:

Oct 2011   Nov 2011   Dec 2011   Jan 2012   Feb 2012 
NumberofPeople NumberofPeople NumberofPeople NumberofPeople NumberofPeople 
EmploymentRate EmploymentRate EmploymentRate EmploymentRate EmploymentRate 
+2

您可以更新標籤以指示您的數據庫類型和版本嗎? – Leigh

回答

3

我已經包括在頂部放置您的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維護列順序,您只需要很好地問它。你應該能夠使用所有這些信息來很好地輸出你的數據。

+0

請記住,同一個「名稱」可能會出現多次,例如會產生多個名爲「John」的列。這可能不是他們的數據問題,只是要注意的事情.. – Leigh

+0

@Dpolehonski 感謝您的幫助,但我仍然面臨一些小問題,雖然。讓我把你的真實案例告訴你。實際上我有Month和Year作爲第一列,然後是第二列,例如顯示「Number of People」,然後是顯示「EmploymentRate」的第三列。 因此,首先顯示列的轉置非常重要: 十月2011十一月2011十二月2011一月2012二月2012 – FlexyBoz

+0

正如你可以看到他們是在一個特定的順序。通過使用你的代碼行: 首先,我得到一個錯誤,因爲空間,其次,如果我不把你的空間提前建議,列表按字母順序排列(應該是這種情況)。 你可以在這裏提供一些進一步的說明。我們可以調整一下你現有的代碼來做到這一點嗎? 非常感謝 – FlexyBoz

1

也許我失去了一些東西,但好像與ORDER BY子句將運行的一個簡單的SQL查詢。例如:

<cfquery name="myquery" datasource="yourdatasourcename"> 
select name, address, age 
from tablename 
order by name 
</cfquery> 

然後在您的ColdFusion輸出頁面中,可以使用具有group屬性的標記。類似這樣的:

<cfoutput query="myquery"> 
<p>name = #name# 
    <cfoutput group="name"> 
    age = #age# 
    address = #address#<br /> 
    </cfoutput> 
</p> 
</cfoutput> 

顯然,您可以根據需要格式化輸出。

編輯 -

如果你是想顯示,如:

Mary  Joe  Sam   Suzie 

28   36  25    42 

123 Maple 16 Oak 3723 Street 832 Busy St. 

也許像(我沒有測試過這一點,只是頭腦風暴):

<cfoutput query="myquery" group="name"> 
<div style="float:left;">name = #name# 
    <cfoutput> 
    <p> 
    age = #age#<br /> 
    address = #address# 
    </p> 
    </cfoutput> 
</div> 
</cfoutput> 
+0

我不認爲這會將名稱轉換爲*列標題*。如果db支持它,那麼在db級別執行它可能會更簡單。 – Leigh

+0

順便說一句,我還沒有測試過你的更新代碼,但'group'屬於頂部的'cfoutput'標籤,以達到你想要的效果。 – Leigh

+0

我通過將'group'屬性移動到另一個'cfoutput'標籤更新了第二個代碼示例。我相信這不會是OP想要的結局,而應該是朝着這個方向邁出的一步。謝謝@Leigh –

0

我想你是在SQL中描述一個數據透視查詢。

+0

是的。不幸的是,他沒有提及他的數據庫類型是否支持「pivot」,或者他的要求是否必須用CF而不是sql來完成。 – Leigh

+0

@Leigh @ Phillips 我正在使用數據庫作爲數據庫。我不知道數據透視表。感謝你們能爲我的案子樹立榜樣。我也嘗試在我身邊:) – FlexyBoz

+0

並非所有的數據庫都支持它們。你在使用哪一個?我不確定'DB'是什麼意思:) DB2? – Leigh