coldfusion - 在 ColdFusion 中显示数据透视查询的结果

标签 coldfusion pivot-table

我有两个这样的表:

验血

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/

相关文章:

php - ColdFusion 中的 PHP 数组相当于什么?

coldfusion - CFHTTP 无法找到请求目标的有效证书路径

python - python 上的数据透视表

php - 如何在链接到一个主键的数据透视表上创建两个外键

python - 使用多索引列展平 DataFrame

java - 在 cfml 中调用用户定义的 java 类

iis-7 - Coldfusion 9 和 IIS7 URL 重写

web-services - API 消耗 Coldfusion

php - Laravel 4 - 将多个值插入数据透视表

php - 从模型中获取所有在数据透视表 Laravel 5 中没有条目的记录