CF SQL Creating a Table with different results

老子叫甜甜 提交于 2019-12-24 16:09:15

问题


I am trying to create a table using coldfusion and sql. The table I am trying to create looks like this:

<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>

The part I am unsure about is how do I loop all of this information under one table? Because you would not want to have the same persons name keep reoccurring on the table and then how do you show how many was generated by them since I could not do something like #associate.recordcount#


回答1:


There seems more than one way to do what you want to achieve like doing a query with joins and groups then dump in table; manage your output with a single CFoutput or use nested CFOutput and/or CFloop. Following show third approach:

<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>

Please note that CF QoQ is case sensitive so if need be then convert associate name and location to lower/upper/title case before hand

A slightly modified code for the CFloop may be like below:

<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>


来源:https://stackoverflow.com/questions/36265968/cf-sql-creating-a-table-with-different-results

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!