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

  • Home
  • SEARCH
  • 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 265075
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T22:44:19+00:00 2026-05-11T22:44:19+00:00

I want to combine three tables – date, lead and click – in a

  • 0

I want to combine three tables – date, lead and click – in a query.

The tables looks like this:

date:

|date|

lead:

id|time|commission

click:

id|time|commission

The table date is just storing dates and is used when getting dates with no click or lead.

So if we have the following data in the tables:

date:

2009-06-01
2009-06-02
2009-06-03

lead:

1|2009-06-01|400
2|2009-06-01|300
3|2009-06-03|350

click:

1|2009-06-01|1
2|2009-06-03|2
3|2009-06-03|2
4|2009-06-03|0

I would like to get date, number of click, commission generated by clicks (there are clicks that don’t give commission), number of leads, commission generated by leads and total commission. So with the tables above I would like to get:

2009-06-01|1|1|2|700|701|
2009-06-02|0|0|0|0|0
2009-06-03|3|4|1|350|354|

I have tried with the following union:

 SELECT  
    campaign_id, 
    commission_date,  
    SUM( click_commission ) AS click_commission,
    click,
    SUM( lead_commission ) AS lead_commission ,  
    lead,
    SUM( total_commission ) as total_commission
    FROM(
        SELECT  
            click.campaign_id AS campaign_id, 
            DATE( click.time ) AS commission_date, 
            click.commission AS click_commission, 
            (SELECT count(click.id) from click GROUP BY date(click.time)) as click,
            0 as lead_commission,
            0 as lead,
            click.commission AS total_commission
        FROM click
        UNION ALL
        SELECT 
            lead.campaign_id AS campaign_id, 
            DATE( lead.time ) AS commission_date, 
            0 as click_commission,
            0 as click,
            lead.commission AS lead_commission, 
            lead.id as lead,
            lead.commission AS total_commission
        FROM lead
        UNION ALL
        SELECT 
            0 AS campaign_id, 
            date.date AS commission_date, 
            0 AS click_commission, 
            0 as click,
            0 AS lead_commission, 
            0 as lead,
            0 AS total_commission
        FROM date 
    ) AS foo 
    WHERE commission_date BETWEEN '2009-06-01' AND '2009-07-25' 
    GROUP BY  commission_date 
    ORDER BY commission_date LIMIT 0, 10

But this does not work to count both the number of clicks and leads, the code above gives the right amount of clicks bot 0 on all leads. If I move the code around and put the select from the lead table I get the leads right bot 0 on all clicks. I have not been able to find a way to get both of the counts from the query.

So I tried a left-join instead:

SELECT
    date.date as date, 
    count( DISTINCT click.id ) AS clicks, 
    sum(click.commission) AS click_commission, 
    count( lead.id ) AS leads, 
    sum(lead.commission) AS lead_commission
FROM date
LEFT JOIN click ON ( date.date = date( click.time ) )
LEFT JOIN lead ON ( date.date = date( lead.time ) )
GROUP BY date.date
LIMIT 0 , 30 

The problem with this query is if there are more than one clicks or leads on a date it will return the expected value * 2. So on 2009-06-01 it will return 1400 instead on the expected 700 for lead commission.

So in the UNION I have problems with the count and in the left join it is the SUM that is not working.

I would really like to stick to the UNION if possible, but I haven’t found a way to get both counts from it.

(This is a follow up to this earlier question, but since I didn’t ask for the count in that I posted a new question.)

  • 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-11T22:44:19+00:00Added an answer on May 11, 2026 at 10:44 pm
    SELECT  date,
            COALESCE(lcomm, 0), COALESCE(lcnt, 0),
            COALESCE(ccomm, 0), COALESCE(ccnt, 0),
            COALESCE(ccomm, 0) + COALESCE(lcomm, 0),
            COALESCE(ccnt, 0) + COALESCE(lcnt, 0)
    LEFT JOIN
            (
            SELECT  date, SUM(commission) AS lcomm, COUNT(*) AS lcnt
            FROM    leads
            GROUP BY
                    date
            ) l
    ON      l.date = d.date
    LEFT JOIN
            (
            SELECT  date, SUM(commission) AS ccomm, COUNT(*) AS ccnt
            FROM    clicks
            GROUP BY
                    date
            ) с
    ON      c.date = d.date
    FROM    date d
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I just want to merge/combine this tables into one query, but it not working.
I have 3 tables. I want to combine these three tables and I need
This is the sequel to this question . I would like to combine three
I have three tables which I want to combine into one view or temporary
I want to combine three sprites and display it as a single sprite. I
I have 2 Tables. 1.Message - Sender_Number,SMS,DateTime. 2.Replies - Receiver_Number,SMS,DateTime. I want to combine
I have two queries which i would like to combine There are 4 tables
In the returned results on a query to a 'Links' table I want to
I have three tables: $sTable = a table of songs (songid, mp3link, artwork, useruploadid
I have two large tables and want to combine all of the column names

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.