在ColdFusion中显示Pivot查询的结果
我有两张这样的桌子: 验血 样品 我使用透视的查询是:在ColdFusion中显示Pivot查询的结果,coldfusion,pivot-table,Coldfusion,Pivot Table,我有两张这样的桌子: 验血 样品 我使用透视的查询是: SELECT * FROM (SELECT testdate, testname, testresult FROM BloodTest NATURAL JOIN Sample PIVOT (MAX(testresult) FOR testname IN ('Na', 'K'))); 这将产生正确的输出,如: testdate 'Na' 'K' 2015-01-01 140
SELECT * FROM (SELECT testdate, testname, testresult
FROM BloodTest
NATURAL JOIN Sample
PIVOT (MAX(testresult) FOR testname IN ('Na', 'K')));
这将产生正确的输出,如:
testdate 'Na' 'K'
2015-01-01 140 4.1
2015-01-02 137 3.8
但是我不知道如何在ColdFusion中显示它-我得到的结果是testresult未定义。我也不知道如何让测试结果在CF表中水平显示
以下是CF代码:
<cfquery name="get_records"
datasource="#Request.DSN#" username="#Request.usrname#" password="#Request.pwd#">
SELECT * FROM (SELECT testdate, testname, testresult FROM BloodTest NATURAL JOIN Sample) PIVOT (MAX(testresult) FOR testname IN ('Na', 'K'))
</cfquery>
<table>
<tr>
<th>Date</th>
<th>Na</th>
<th>K</th>
</tr>
<cfoutput query="get_records">
<tr>
<td>#testdate#</td>
<td>#testresult#</td>
<td><!-- how to get 2nd testresult here --></td>
</tr>
</cfoutput>
我得到的错误是:
变量TESTRESULT未定义
The error occurred in /data/coldfusion11/cfusion/wwwroot/blood.cfm: line 37
35 : <tr>
36 : <td>#testdate#</td>
37 : <td>#testresult#</td>
38 : </tr>
39 : </cfoutput>
讽刺的是,今天早上我向一位正在学习CF的同事展示了以下技巧
<cfquery name="pivotQueryData">
sql goes here
</cfquery>
现在我们将把它输出到一个html表中
<cfoutput>
<table>
<tr>
<cfloop array="#pivotQueryData.getcolumnlist()#" index="header">
<th>#Header#</th>
</cfloop>
</tr>
<cfloop query="pivotQueryData">
<tr>
<cfloop array="#pivotQueryData.getcolumnlist()#" index="field">
<td>#pivotQueryData[field][currentrow]#</td>
</cfloop>
</tr>
</cfloop>
</table>
</cfoutput>
testresult未定义。查看cfquery的cfdump和用于输出查询结果的代码会有所帮助添加一些附加信息由于您使用的是PIVOT,结果将包含在列名中:K和NA not testresult。因此,您需要使用这些列名来代替ie NA和K。是的,由于数据透视生成的列名可能包含无效字符,因此使用关联数组表示法是一种很好的方法。请记住query.getColumnList是未记录的。另一种选择是核心功能。它返回一个结构数组,其中包含有关查询列的详细信息:IsCaseSensitive、Name、TypeName。
<cfquery name="pivotQueryData">
sql goes here
</cfquery>
<cfoutput>
<table>
<tr>
<cfloop array="#pivotQueryData.getcolumnlist()#" index="header">
<th>#Header#</th>
</cfloop>
</tr>
<cfloop query="pivotQueryData">
<tr>
<cfloop array="#pivotQueryData.getcolumnlist()#" index="field">
<td>#pivotQueryData[field][currentrow]#</td>
</cfloop>
</tr>
</cfloop>
</table>
</cfoutput>