Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 3936044
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 19, 20262026-05-19T23:53:59+00:00 2026-05-19T23:53:59+00:00

I’m trying to write a query that selects from four tables campaignSentParent csp campaignSentEmail

  • 0

I’m trying to write a query that selects from four tables

  • campaignSentParent csp
  • campaignSentEmail cse
  • campaignSentFax csf
  • campaignSentSms css

Each of the cse, csf, and css tables are linked to the csp table by csp.id = (cse/csf/css).parentId

The csp table has a column called campaignId,

What I want to do is end up with rows that look like:

| id | dateSent   | emailsSent | faxsSent | smssSent |  
| 1  | 2011-02-04 | 139        | 129      | 140      |  

But instead I end up with a row that looks like:

| 1  | 2011-02-03 | 2510340    | 2510340  | 2510340  |

Here is the query I am trying

SELECT csp.id id, csp.dateSent dateSent,  
       COUNT(cse.parentId) emailsSent,  
       COUNT(csf.parentId) faxsSent,  
       COUNT(css.parentId) smsSent  
FROM   campaignSentParent csp,  
       campaignSentEmail cse,  
       campaignSentFax csf,  
       campaignSentSms css  
WHERE  csp.campaignId = 1  
AND    csf.parentId = csp.id  
AND    cse.parentId = csp.id  
AND    css.parentId = csp.id;  

Adding GROUP BY did not help, so I am posting the create statements.

csp

CREATE TABLE `campaignsentparent` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `campaignId` int(11) NOT NULL,
  `dateSent` datetime NOT NULL,
  `account` int(11) NOT NULL,
  `status` varchar(15) NOT NULL DEFAULT 'Creating',
  PRIMARY KEY (`id`)
) ENGINE=MyISAM AUTO_INCREMENT=2 DEFAULT CHARSET=latin1

cse/csf (same structure, different names)

CREATE TABLE `campaignsentemail` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `parentId` int(11) NOT NULL,
  `contactId` int(11) NOT NULL,
  `content` text,
  `subject` text,
  `status` varchar(15) DEFAULT 'Pending',
  PRIMARY KEY (`id`)
) ENGINE=MyISAM AUTO_INCREMENT=140 DEFAULT CHARSET=latin1

css

CREATE TABLE `campaignsentsms` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `parentId` int(11) NOT NULL,
  `contactId` int(11) NOT NULL,
  `content` text,
  `status` varchar(15) DEFAULT 'Pending',
  PRIMARY KEY (`id`)
) ENGINE=MyISAM AUTO_INCREMENT=141 DEFAULT CHARSET=latin1
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-19T23:54:00+00:00Added an answer on May 19, 2026 at 11:54 pm

    You need to aggregate the sums separately, not as shown in the question.

    SELECT csp.id, csp.dateSent dateSent,  
           e.email_count, f.fax_count, s.sms_count
      FROM campaignSentParent AS csp
      JOIN (SELECT cse.ParentId, COUNT(*) AS email_count
              FROM campaignSentEmail cse
             GROUP BY cse.ParentID) AS e ON e.parentID = csp.id
      JOIN (SELECT csf.ParentId, COUNT(*) AS fax_count
              FROM campaignSentFax csf
             GROUP BY csf.ParentID) AS f ON f.ParentID = csp.id
      JOIN (SELECT css.ParentID, COUNT(*) AS sms_count
              FROM campaignSentSms css
             GROUP BY css.ParentId) AS s ON s.ParentID = csp.id
     WHERE csp.campaignId = 1  
    

    To do this, you pretty much have to use the JOIN notation as shown.

    You depending on the quality of your optimizer and the cardinalities of the various tables and the available indexes, you might find it effective to include a join with CampaignSentParent in each of the sub-queries with the csp.CampaignID = 1 condition, so as to limit the data aggregated by the sub-queries.


    You might notice that the result count you get is 2510340. The prime factorization of 2510340 is 2 × 2 × 3 × 5 × 7 × 43 × 139, and your expected answer is 139, 129, and 140. You can get 3 × 43 = 129; 2 × 2 × 5 × 7 = 140; and 139 = 139. In other words, the original query is generating the Cartesian product of all the rows in the three dependent tables and counting the product, rather than counting the relevant rows from each dependent table separately.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to decode HTML entries from here NYTimes.com and I cannot figure out
I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this
I am trying to understand how to use SyndicationItem to display feed which is
Basically, what I'm trying to create is a page of div tags, each has
link Im having trouble converting the html entites into html characters, (&# 8217;) i
Does anyone know how can I replace this 2 symbol below from the string
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I have a bunch of posts stored in text files formatted in yaml/textile (from
I am trying to loop through a bunch of documents I have to put
I'm new to using the Perl treebuilder module for HTML parsing and can't figure

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.