是否可以 运行 SQL 查询一次并包括详细输出和按类别计数?

Is it possible to run SQL query once and include both detail output and a count by category?

是否可以运行一个SQL一次并显示详细输出+按名称显示金额计数?下面的代码按我的意愿工作,但是它 运行 有两个查询,所以这可能不会那么有效..

    <cfquery name="myta" datasource="zett"> 
    Select w.t_firstname, w.t_lastname, w.t_total
    from table_bst a, table_zr w
    where 1=1
    ....
    </cfquery> 


    <cfquery name="meto" datasource="zett"> 
    SELECT a.t_firstname as Firstname,  COUNT(*)  AS Status
    from table_bst a, table_zr w
    where 1=1
    ....
    </cfquery> 

        <cfif meto.recordcount EQ 0>
            <table><tr><td style="color:#FF0000">There is currently an error</td></tr></table>
        <cfelse>
            <cfoutput>
                <table>
                  <cfloop query="meto">
                  <cfset temp = ValueList(myta.status)
                 <tr><td>FirstnameList: <cfoutput>#ListLen(temp)#</cfoutput></td></tr>
                </table>
           </cfoutput>
        </cfif> 

完成您正在寻找的最简单且以 ColdFusion 为中心的方法是简单地使用查询查询:

主要查询:

<cfquery name="myta" datasource="zett"> 
    SELECT w.t_firstname, w.t_lastname, w.t_total
    FROM table_bst a
    INNER (OR LEFT/RIGHT OUTER) JOIN table_zr w ON a.? = w.?
    WHERE
        [ 
          1=1 is only really useful if you have some sort of if/else logic in your 
          WHERE clause, and it's used to make sure there is a value there if none 
          of the if/else conditions are met.
        ]
        AND/OR [other stuff]
</cfquery> 

查询之查询:

<cfquery name="meto" dbtype="query">
    SELECT t_firstname AS FirstName, count(*) AS Status
    FROM myta
    GROUP BY FirstName
</cfquery>

输出:

<table>
    <cfif meto.recordcount EQ 0>
        <tr><td style="color:#FF0000">There is currently an error</td></tr>
    <cfelse>
        <cfoutput query="meto">
            <tr>
                <td>#Firstname#:#Status#</td>
            </tr>
        </cfoutput>
    </cfif> 
</table>

https://trycf.com/gist/529c0bf10b7c6d7420eae399572744bb/lucee5?theme=monokai