ColdFusion中的转置查询

ColdFusion中的转置查询,coldfusion,coldfusion-9,cfquery,Coldfusion,Coldfusion 9,Cfquery,我有一个简单的cfquery,它输出3列及其各自的数据。列包括姓名、地址和年龄 我想转置这组数据,使名称成为列,地址和年龄显示在每列下 我知道我们可以在这个问题上使用QueryAddColumn或类似的东西。有人能帮我解决这个问题吗 编辑: 根据以下评论,这是预期输出: Oct 2011 Nov 2011 Dec 2011 Jan 2012 Feb 2012 NumberofPeople NumberofPeople

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

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

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

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

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

也许我遗漏了一些东西,但似乎一个带有ORDERBY子句的简单SQL查询就可以了。大概是这样的:

<cfquery name="myquery" datasource="yourdatasourcename">
select name, address, age
from tablename
order by name
</cfquery>
<cfoutput query="myquery">
<p>name = #name#
    <cfoutput group="name">
    age = #age#
    address = #address#<br />
    </cfoutput>
</p>
</cfoutput>
也许类似(我没有测试过,只是头脑风暴):


name=#name#

年龄=#年龄#
地址=#地址#


也许我遗漏了一些东西,但似乎一个带有ORDERBY子句的简单SQL查询就可以了。大概是这样的:

<cfquery name="myquery" datasource="yourdatasourcename">
select name, address, age
from tablename
order by name
</cfquery>
<cfoutput query="myquery">
<p>name = #name#
    <cfoutput group="name">
    age = #age#
    address = #address#<br />
    </cfoutput>
</p>
</cfoutput>
也许类似(我没有测试过,只是头脑风暴):


name=#name#

年龄=#年龄#
地址=#地址#


我在顶部包含了一个示例数据行,您可以在其中放置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())#">

我会这样做,但我想不出为什么我会这样做。我很想听听你的用例。不管怎样,我希望这有帮助

(注意,这是在CF9中测试的,所以您应该能够复制并粘贴它来自己测试。)

编辑-(再次):

忘了提一下,这只能在从DB中检索的名称是有效列名的情况下工作,因此没有空格(在本例中,日期中的空格已替换为下划线)


>>新代码段对于更新的数据结构,函数
valueList(firstQuery.date,,')
不会对列重新排序。转储时,列将在输出时重新排序。我使用函数
ArrayToList(newQuery.getColumnNames())
表明CF在内部维护了列顺序,您只需很好地询问即可。您应该能够使用所有这些信息很好地输出您需要的数据。

我在顶部包含了一个示例数据行,您可以将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())#">

我会这样做,但我想不出为什么我会这样做。我很想听听你的用例。不管怎样,我希望这有帮助

(注意,这是在CF9中测试的,所以您应该能够复制并粘贴它来自己测试。)

编辑-(再次):

忘了提一下,这只能在从DB中检索的名称是有效列名的情况下工作,因此没有空格(在本例中,日期中的空格已替换为下划线)



>>新代码段对于更新的数据结构,函数
valueList(firstQuery.date,,')
不会对列重新排序。转储时,列将在输出时重新排序。我使用函数
ArrayToList(newQuery.getColumnNames())
表明CF在内部维护了列顺序,您只需很好地询问即可。您应该能够使用所有这些信息以您需要的方式很好地输出数据。

我认为您是在用SQL描述数据透视查询。

我认为您是在用SQL描述数据透视查询。

您能更新标记以指示您的数据库类型和版本吗?您能更新标记以指示您的数据库类型和版本吗?我不能我认为这会将名称转换为列标题。如果db支持它,在db级别执行可能会更简单。顺便说一句,我没有测试更新的代码,但是
属于顶部的
cfoutput
标记,以达到您想要的效果。我通过将'group'属性移动到另一个'cfoutput'标记来更新第二个代码示例。我相信这不是OP想要的最终观点,而是朝着这个方向迈出的一步。谢谢@Leigh@Mike感谢您编辑我的显示输出。我试着那样做,但做不到。我删除了关于它现在显示不正确的评论。我认为这不会将名称转换为列标题。如果db支持它,在db级别执行可能会更简单。顺便说一句,我没有测试更新的代码,但是
属于顶部的
cfoutput
标记,以达到您想要的效果。我通过将'group'属性移动到另一个'cfoutput'标记来更新第二个代码示例。我相信这不是OP想要的最终观点,而是朝着这个方向迈出的一步。谢谢@Leigh@Mike感谢您编辑我的显示输出。我试着那样做,但做不到。我删除了关于它现在显示不正确的评论。请记住,同一个“名称”可能出现多次,例如,这将产生多个名为“John”的列。他们的数据可能没有问题,只是需要注意。@Dpolehonski谢谢你的帮助,但我仍然面临一些小问题。让我向你说明我的真实情况。实际上,我将月份和年份作为第一列,然后是第二列,显示例如“NumberofPeople”,然后是第三列,显示“EmploymentRate”。因此,首先,重要的是显示列的格式:2011年10月11月2011年12月2012年1月2012年2月2012,因为您可以看到它们是按特定顺序排列的。通过使用你的代码行:首先,由于空格,我得到了一个错误,其次,如果我没有像你之前建议的那样放置空格,列列表是按字母顺序排列的(应该是这样)。你能进一步解释一下吗。我们可以稍微调整一下您现有的代码来做到这一点吗?非常感谢
query。columnList
总是按字母顺序排列的。您可以使用
getMetaData(query)
返回的数组以原始顺序遍历列。至于空格,可以用下划线“\ux”之类的字符代替。然后在输出时恢复它