在ColdFusion中转置查询

时间:2012-09-11 12:55:25

标签: coldfusion coldfusion-9 cfquery

我有一个简单的cfquery,它输出3列及其各自的数据。列是姓名,地址和年龄。

我想转置这组数据,以便名称成为列,并在每列下显示地址和年龄。

我知道我们可以使用QueryAddColumn或类似的东西来解决这个问题。有人可以帮我解决这个问题吗?

编辑: 根据下面的评论,这是预期的输出:

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

3 个答案:

答案 0 :(得分: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())#">

这是我怎么做的,但我想不出为什么我会这样做。我有兴趣听听你的用例。无论如何,我希望这会有所帮助。

(P.S这是在CF9中测试的,所以你应该能够复制并粘贴它以便自己测试。)

编辑 - (再次):

忘记提及,这只有在您从DB检索的名称是有效列名时才有效,因此没有空格(在此示例中,日期中的空格已被下划线替换)!

&gt;&gt;&gt; 更新后的数据结构的新代码段,函数valueList(firstQuery.date,',')不会重新排序您的列。在转储时,列在输出上重新排序。我使用函数ArrayToList(newQuery.getColumnNames())来表明内部CF维护列顺序,你只需要很好地问它。您应该能够使用所有这些信息,以便在需要时很好地输出数据。

答案 1 :(得分: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>

答案 2 :(得分:0)

我认为您在SQL中描述了一个数据透视查询。