count total records for each user

风格不统一 提交于 2019-12-11 06:05:51

问题


I have this code that pulls all users with open requests. I need to show a total for each assigned buyer.

<CFQUERY  NAME="allbuyers" DATASOURCE="procuresource3">
    SELECT *
    FROM allorders3
    WHERE open = 'y'  
    order by assignedbuyer
</cfquery>

<cfoutput query="allbuyers">  
    #assignedbuyer# #prn#
</cfoutput>

<cfoutput>
    Total: #allbuyers.RecordCount# 
</cfoutput> 

The output is all records. I need a total for each user. Something like this below:

Christine - 301366
Christine - 300729
Christine - 300731
Christine - 300732
Total: 4
<br>
Connie    - 301524
Connie    - 301516
Connie    - 301510
Connie    - 301509
Connie    - 301460
Connie    - 301362
Total: 6
<br>
Dannette  - 301478
Dannette  - 301458
Dannette  - 301340
Total: 3

Thank you in advance. CarlosL


回答1:


There are multiple ways to accomplish this with query adjustments to get counts and building up struct counts by unique key.

The most straight forward with where you are currently is to build up a count based on whether the current assignedbuyer is the same as the previous assignedbuyer. CF provides a built-in way to have code run in a loop when a query group column changes.

<cfset q = queryNew("") />
<cfset queryAddColumn(q, "name", "varchar", ['Dan','Dan', 'Bob', 'Bob', 'Bob', 'Chris']) />

<cfoutput query="q" group="name">
    <cfset count = 0 />
    <p>
        <cfoutput>
            #name#<br />
            <cfset count += 1 />
        </cfoutput>
        Total: #count#
    </p>
</cfoutput>

Output

<p>
    Dan<br>
    Dan<br>
    Total: 2
</p>
<p>
    Bob<br>
    Bob<br>
    Bob<br>
    Total: 3
</p>
<p>
    Chris<br>
    Total: 1
</p>


来源:https://stackoverflow.com/questions/58292615/count-total-records-for-each-user

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