我有两个这样的表:
验血
testid, testname
示例
sampleid, testid, testresult, testdate
我使用数据透视表的查询是:
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>
关于coldfusion - 在 ColdFusion 中显示数据透视查询的结果,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33876129/