1

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

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

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

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

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

3 回答 3

3

I have included a sample data row at the top where you would put your cfquery statement.

<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())#">

This is How I would Do it, But I can't think of why I would do it. I'd be interested to hear your use case. Anyway, I hope this helps.

(P.S This is tested in CF9, so you should be able to copy and paste it to test for yourself.)

EDIT -(Again):

Forgot to mention, this can only work if the names your retrieveing from the DB are valid column names, so no spaces (In this example spaces in dates have been replaced by underscores)!

>>> New code snippet for the updated data structure, the function valueList(firstQuery.date,',') doesn't re-order your columns. The columns are re-ordered on output when dumping. I have used the function ArrayToList(newQuery.getColumnNames()) to show that internally CF maintains the column order and you need only ask it nicely. You should be able to use all this information to nicely output your data how you need it.

于 2012-09-11T13:58:42.833 回答
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>
于 2012-09-11T13:23:09.980 回答
0

我认为您正在描述 SQL 中的数据透视查询。

于 2012-09-12T03:39:09.313 回答