Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Query of Queries Union Not Totaling Across Fields

Status
Not open for further replies.

Ceilicat

Programmer
Feb 25, 2004
1
0
0
US
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>
 
You need to tweak your SQL. UNIONs don't sum.

As a quick-and-dirty:
SELECT esp, SUM(instances)
FROM
(
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
) as instances
GROUP BY esp
ORDER BY esp ASC

-----------
With business clients like mine, you'd be better off herding cats.
 
One a more fundamental note - this points out one of the advantages to keeping your databases in third normal. If you had a related table where you kept email addresses you could have as many or as few as you wanted per user and you wouldn't have a bunch of fields with null values - and - these kinds of reports would be easier to write.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top