Jump to content

Need to combine totals of two table columns


n1concepts
Go to solution Solved by Barand,

Recommended Posts

I performed a UNION on two tables - 'leads' and 'ext_leads' to group several columns which are identical but one in particular which is an aggregate.

Note: on both, I need to get total count of that one column and on both I named the alias total_leads - see UNION query below:

SELECT DATE(l.time1) as date,a.affid,a.aname,
COUNT(l.reqid) as total_leads FROM leads l 
LEFT JOIN affiliates AS a ON l.affid = a.affid 
LEFT JOIN campaign AS c ON l.campid = c.campid 
WHERE (a.affid = l.affid) 
GROUP BY DATE(l.time1),a.aname 
UNION ALL 
SELECT DATE(x.extstamp) as date,a.affid,a.aname,
COUNT(x.reqid) as total_leads FROM ext_leads x 
LEFT JOIN clicks AS cl ON cl.reqid = x.reqid
LEFT JOIN affiliates AS a ON a.affid = cl.affid_sid    
WHERE (cl.reqid = x.reqid)    
GROUP BY DATE(x.extstamp),a.aname 
ORDER BY date;

And the results is as follow which pulls one row - per 'aname' from each of the two tables - 'leads' and 'ext_leads'.

However, I need to take this one step further and combine the totals pulled (per aname) from 'leads' with those for same 'aname' from 'ext_leads'

+------------+-------+---------------------------+-------------+
| date       | id    | aname                     | total_leads |
+------------+-------+---------------------------+-------------+
| 2013-05-27 |   121 | ABC, LLC                  |         145 |
| 2013-05-27 |   121 | ABC, LLC                  |          33 |
| 2013-05-29 |   122 | Company x                 |          67 |
| 2013-05-29 |   122 | Company x                 |           2 |
+------------+-------+---------------------------+-------------+
2 rows in set

=======

What I need is to combine the total leads from the 'leads' & 'ext_leads' tables so that:
ABC, LLC total for 5/27 show 178 and Company x totals is 69.

I tried SUM() function and calling the (above) query as a view but getting cross-joins so need some advise on how to merge the two totals and group by 'aname' column sorting by date - descending to get desired results.

 

Any help to form final query to acheive goal truly appreciated - thx!
MYSQL version: 5.3

Link to comment
Share on other sites

  • Solution

try

SELECT date, aname, SUM(total_leads) as total
FROM
    (
    SELECT DATE(l.time1) as date,a.affid,a.aname,
        COUNT(l.reqid) as total_leads FROM leads l 
        LEFT JOIN affiliates AS a ON l.affid = a.affid 
        LEFT JOIN campaign AS c ON l.campid = c.campid 
        WHERE (a.affid = l.affid) 
        GROUP BY DATE(l.time1),a.aname 
    UNION ALL 
    SELECT DATE(x.extstamp) as date,a.affid,a.aname,
        COUNT(x.reqid) as total_leads FROM ext_leads x 
        LEFT JOIN clicks AS cl ON cl.reqid = x.reqid
        LEFT JOIN affiliates AS a ON a.affid = cl.affid_sid    
        WHERE (cl.reqid = x.reqid)    
        GROUP BY DATE(x.extstamp),a.aname
    ) as sub
ORDER BY date, aname
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.