Background: I am using ColdFusion to access an MS-Access database table via SQL. I am a novice at this.
Situation: I have a membership list database table (.MDB MS-Access, accessed via <CFQUERY>) with four email address fields (email1, email 2, email3, email4) - ALL N SAME TABLE. (Some folks have multiple email addresses, often from different Email Service Providers - ESPs.)
I need to develop an SQL query (or series of queries) to produce a list of all the ESPs (e.g. gmail.com, comcast.net, etc.) along with a count of how many of each there are, across ALL FOUR of these fields. As an example, the desired end result set SHOULD be a single list that looks like:
comcast.net (6)
gmail.com (14)
cs.com (1)
hotmail.com (4)
att.net (12)
aol.com (2)
. . . .
etc.
So far, I am able to do this for each field individually, but when I created the following QoQ to UNION across all four fields, I get close to what I want, but it is listing the totals for each individual field instead of adding them up. So, I get:
att.net (2)
att.net (3)
att.net (9)
att.net (13)
aol.com (6)
aol.com (4)
aol.com (1)
aol.com (8)
. . .
etc.
instead of the desired:
att.net (27)
aol.com (19)
. . .
etc.
How can I get them to combine all together? Thanks for your help! Here is my code:
<!--- get list of ESP domain names (e.g. comcast.net) from members' email addresses --->
<!--- first, get member email addresses - up to four addresses --->
<!--- there are four fields, all in SAME table --->
<!--- email address one --->
<cfquery name="cfqGetESP1" datasource="#member_datasource#">
SELECT MID(email1, INSTR(email1, '@') + 1)
AS esp
FROM tblMembers
WHERE email1 IS NOT NULL
</cfquery>
<!--- email address two --->
<cfquery name="cfqGetESP2" datasource="#member_datasource#">
SELECT MID(email2, INSTR(email2, '@') + 1)
AS esp
FROM tblMembers
WHERE email2 IS NOT NULL
</cfquery>
<!--- email address three --->
<cfquery name="cfqGetESP3" datasource="#member_datasource#">
SELECT MID(email3, INSTR(email3, '@') + 1)
AS esp
FROM tblMembers
WHERE email3 IS NOT NULL
</cfquery>
<!--- email address four --->
<cfquery name="cfqGetESP4" datasource="#member_datasource#">
SELECT MID(email4, INSTR(email4, '@') + 1)
AS esp
FROM tblMembers
WHERE email4 IS NOT NULL
</cfquery>
<!--- QoQ to get COMBINED total instances of EACH ESP --->
<!--- across ALL four email address fields --->
<cfquery name="cfqCountESP" dbtype="query">
SELECT esp, COUNT(esp) AS instances
FROM cfqGetESP1
GROUP BY esp
UNION
SELECT esp, COUNT(esp) AS instances
FROM cfqGetESP2
GROUP BY esp
UNION
SELECT esp, COUNT(esp) AS instances
FROM cfqGetESP3
GROUP BY esp
UNION
SELECT esp, COUNT(esp) AS instances
FROM cfqGetESP4
GROUP BY esp
ORDER BY esp ASC
</cfquery>
<!--- test display --->
<p>TEST DISPLAY COUNT ESP ACROSS ALL EMAIL ADDRESS FIELDS<br>
<cfoutput>
<cfloop query="cfqCountESP">
#cfqCountESP.esp# (#instances#)<br>
</cfloop>
</cfoutput>
Situation: I have a membership list database table (.MDB MS-Access, accessed via <CFQUERY>) with four email address fields (email1, email 2, email3, email4) - ALL N SAME TABLE. (Some folks have multiple email addresses, often from different Email Service Providers - ESPs.)
I need to develop an SQL query (or series of queries) to produce a list of all the ESPs (e.g. gmail.com, comcast.net, etc.) along with a count of how many of each there are, across ALL FOUR of these fields. As an example, the desired end result set SHOULD be a single list that looks like:
comcast.net (6)
gmail.com (14)
cs.com (1)
hotmail.com (4)
att.net (12)
aol.com (2)
. . . .
etc.
So far, I am able to do this for each field individually, but when I created the following QoQ to UNION across all four fields, I get close to what I want, but it is listing the totals for each individual field instead of adding them up. So, I get:
att.net (2)
att.net (3)
att.net (9)
att.net (13)
aol.com (6)
aol.com (4)
aol.com (1)
aol.com (8)
. . .
etc.
instead of the desired:
att.net (27)
aol.com (19)
. . .
etc.
How can I get them to combine all together? Thanks for your help! Here is my code:
<!--- get list of ESP domain names (e.g. comcast.net) from members' email addresses --->
<!--- first, get member email addresses - up to four addresses --->
<!--- there are four fields, all in SAME table --->
<!--- email address one --->
<cfquery name="cfqGetESP1" datasource="#member_datasource#">
SELECT MID(email1, INSTR(email1, '@') + 1)
AS esp
FROM tblMembers
WHERE email1 IS NOT NULL
</cfquery>
<!--- email address two --->
<cfquery name="cfqGetESP2" datasource="#member_datasource#">
SELECT MID(email2, INSTR(email2, '@') + 1)
AS esp
FROM tblMembers
WHERE email2 IS NOT NULL
</cfquery>
<!--- email address three --->
<cfquery name="cfqGetESP3" datasource="#member_datasource#">
SELECT MID(email3, INSTR(email3, '@') + 1)
AS esp
FROM tblMembers
WHERE email3 IS NOT NULL
</cfquery>
<!--- email address four --->
<cfquery name="cfqGetESP4" datasource="#member_datasource#">
SELECT MID(email4, INSTR(email4, '@') + 1)
AS esp
FROM tblMembers
WHERE email4 IS NOT NULL
</cfquery>
<!--- QoQ to get COMBINED total instances of EACH ESP --->
<!--- across ALL four email address fields --->
<cfquery name="cfqCountESP" dbtype="query">
SELECT esp, COUNT(esp) AS instances
FROM cfqGetESP1
GROUP BY esp
UNION
SELECT esp, COUNT(esp) AS instances
FROM cfqGetESP2
GROUP BY esp
UNION
SELECT esp, COUNT(esp) AS instances
FROM cfqGetESP3
GROUP BY esp
UNION
SELECT esp, COUNT(esp) AS instances
FROM cfqGetESP4
GROUP BY esp
ORDER BY esp ASC
</cfquery>
<!--- test display --->
<p>TEST DISPLAY COUNT ESP ACROSS ALL EMAIL ADDRESS FIELDS<br>
<cfoutput>
<cfloop query="cfqCountESP">
#cfqCountESP.esp# (#instances#)<br>
</cfloop>
</cfoutput>