INTELLIGENT WORK FORUMS
FOR COMPUTER PROFESSIONALS

Log In

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!

*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Jobs

Query of Queries Union Not Totaling Across Fields

Query of Queries Union Not Totaling Across Fields

(OP)
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>

RE: Query of Queries Union Not Totaling Across Fields

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.

RE: Query of Queries Union Not Totaling Across Fields

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.

Red Flag This Post

Please let us know here why this post is inappropriate. Reasons such as off-topic, duplicates, flames, illegal, vulgar, or students posting their homework.

Red Flag Submitted

Thank you for helping keep Tek-Tips Forums free from inappropriate posts.
The Tek-Tips staff will check this out and take appropriate action.

Reply To This Thread

Posting in the Tek-Tips forums is a member-only feature.

Click Here to join Tek-Tips and talk with other members!

Resources

Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close