Mike Vierow
02/11/2022, 11:04 PM<cfquery name="test">select firstname from names order by firstname asc</cfquery>
<cfoutput query="test" group="firstname">
<cfoutput>
#test.recordcount# <!--- total number of people with this first name --->
</cfoutput>
</cfoutput>
Mark Takata (Adobe)
02/11/2022, 11:11 PMMike Vierow
02/11/2022, 11:12 PMMike Vierow
02/11/2022, 11:14 PMMark Takata (Adobe)
02/11/2022, 11:16 PMAdam Cameron
<cfscript>
people = queryNew("id,firstName,lastName", "integer,varchar,varchar", [
[1, "Anna", "Apple"],
[2, "Anna", "Banana"],
[3, "Brian", "Cherry"],
[4, "Claire", "Date"],
[5, "Claire", "Eggplant"],
[6, "Claire", "Fig"],
[7, "Dave", "Grape"]
])
namePopularity = people.reduce((names, person) => {
return names.insert(
person.firstName,
names.keyExists(person.firstName) ? ++names[person.firstName] : 1,
true
)
}, {})
</cfscript>
<cfoutput query="people" group="firstname">
#firstName#: #namePopularity[firstName]#<br>
<ul><cfoutput><li>#lastName#</li></cfoutput></ul>
</cfoutput>
Result:
Anna: 2
* Apple
* Banana
Brian: 1
* Cherry
Claire: 3
* Date
* Eggplant
* Fig
Dave: 1
* Grape
Mike Vierow
02/11/2022, 11:52 PMpeople
?Adam Cameron
SELECT firstName, COUNT(1) FROM people GROUP BY firstName
as well as the query to get the actual data. There will be a point at which the overhead of the second DB hit would become less than the overhead of doing the reduce
on a big recordset, yeah. I'd try both 😉Adam Cameron
Scott Bennett
02/12/2022, 1:43 AM<cfquery name="test">select firstname from names order by firstname asc</cfquery>
<cfoutput query="test" group="firstname">
<cfset thisnamecount=0>
<cfoutput>
<cfset thisnamecount++>
</cfoutput>
#test.firstname# #thisnamecount#
</cfoutput>
aliaspooryorik
aliaspooryorik
aliaspooryorik
Mark Takata (Adobe)
02/12/2022, 6:38 PM