CF SQL创建具有不同结果的表

时间:2016-03-28 15:57:42

标签: coldfusion

我正在尝试使用coldfusion和sql创建一个表。我试图创建的表格如下:

<cfquery datasource="#application.dsn#" name="someprocessTable">
    SELECT *
    FROM checklists
</cfquery>

<table id="Checklist_Stats">
    <thead>
        <th><b>Associate Name</b></th>
        <th><b>Location</b></th>
        <th><b>Checklists Generated by Associate</b></th>
        <th><b>Checklists Generated by Selected Location(s)</b></th>
        <th><b>Associate Percentage of Location Total</b></th>   
    </thead>
    <tbody>
        <cfoutput query="someprocessTable">                     
            <tr>
                <td>#associate#</td>
                <td>#location_code#</td>
                <td>#associate.recordcount#</td>
                <!---<td>##</td>
                <td>##</td>--->
            </tr>                      
        </cfoutput>
    </tbody>
</table>

我不确定的部分是如何在一个表格下循环所有这些信息?因为你不想让同一个人的名字继续在桌面上重复出现,然后你如何显示他们生成了多少人,因为我不能做#associate.recordcount#

之类的事情。

1 个答案:

答案 0 :(得分:1)

使用连接和组进行查询然后转储到表中,似乎有多种方法可以实现您想要实现的目标;使用单个CFoutput管理输出或使用嵌套的CFOutput和/或CFloop。 以下是第三种方法:

<table border="1" id="Checklist_Stats">
    <thead>
        <th><b>Associate Name</b></th>
        <th><b>Location</b></th>
        <th><b>Checklists Generated by Associate</b></th>
        <th><b>Checklists Generated by Selected Location(s)</b></th>
        <th><b>Associate Percentage of Location Total</b></th>   
    </thead>
    <tbody>
    <cfquery name="allAssociatesQry" dbtype="query">
        SELECT DISTINCT associate, COUNT(*) AS associateCount FROM someprocessTable GROUP BY associate ORDER BY associate 
    </cfquery>
    <cfloop query="allAssociatesQry">
        <cfquery name="allLocCodeForAssociateQry" dbtype="query">
            SELECT * FROM someprocessTable WHERE associate='#associate#' ORDER BY location_code
        </cfquery>
        <tr><td><cfoutput>#allLocCodeForAssociateQry.associate#</cfoutput></td>
        <cfoutput query="allLocCodeForAssociateQry" group="location_code">
            <cfset locCntr = 0 />
            <cfoutput>
                <cfset locCntr = locCntr + 1 />
            </cfoutput>
            <cfif allLocCodeForAssociateQry.currentRow NEQ 1>
                <tr><td>&nbsp;</td>
            </cfif>
                <td>#allLocCodeForAssociateQry.location_code#</td>
                <td>#allAssociatesQry.associateCount#</td>
                <td>#locCntr#</td>
                <td>#Round((locCntr/allAssociatesQry.associateCount) * 100)#%</td>
            </tr>                      
        </cfoutput>
    </cfloop>
    </tbody>
</table>

请注意,CF QoQ区分大小写,因此如果需要,请将关联名称和位置转换为较低/较高/标题大小写

对CFloop稍加修改的代码可能如下所示:

<cfloop query="allAssociatesQry">
    <cfset thisAssociateName = trim(allAssociatesQry.associate) />
    <cfquery name="allLocCodeForAssociateQry" dbtype="query">
        SELECT location_code,count(location_code) AS locCntr FROM someprocessTable WHERE associate='#thisAssociateName#' GROUP BY location_code ORDER BY location_code
    </cfquery>
    <cfoutput query="allLocCodeForAssociateQry">
        <tr>
            <td>#thisAssociateName#</td>
            <td>#allLocCodeForAssociateQry.location_code#</td>
            <td>#allAssociatesQry.associateCount#</td>
            <td>#allLocCodeForAssociateQry.locCntr#</td>
            <td>#Round((allLocCodeForAssociateQry.locCntr/allAssociatesQry.associateCount) * 100)#%</td>
        </tr>
        <cfset thisAssociateName = "" />
    </cfoutput>
</cfloop>