Paige Rose Figueira
Paige Rose Figueira

Reputation: 121

SELECT COUNT() in a left join table mysql

I am attempting to count the number of new leads and the number of closed leads in any given month. The query below returns the month array correctly, but then only counts 1 if there are any leads or closes in the months, instead of the actual number that happened in that month. Any ideas what I am doing wrong?

query:

$productSql = "SELECT months.monthnum as monthnum, COUNT(contacts.lead_date) as lead_date_month, COUNT(contacts.close_date) as close_date_month, contacts.rep as rep 
                FROM months 
                LEFT JOIN contacts 
                ON months.monthnum = MONTH(contacts.lead_date) 
                    AND months.monthnum = MONTH(contacts.close_date) 
                    AND contacts.compid='$compid' 
                    AND YEAR(contacts.lead_date) = '$year' 
                    AND YEAR(contacts.close_date) = '$year' 
                    AND contacts.rep = '$rep' 
                GROUP BY months.monthnum 
                ORDER BY months.monthnum ASC";

$productResult = mysql_query($productSql, $link) or die(mysql_error());


$label[] = $productRow['monthnum'];
$lead[] = $productRow['lead_date_month'];
$close[] = $productRow['close_date_month'];
};

echo "[".json_encode($label).",".json_encode($lead).",".json_encode($close)."]";

result:

[["January","February","March","April","May","June","July","August","September","October","November","December"],
["0","0","0","0","0","0","0","0","1","1","0","0"],
["0","0","0","0","0","0","0","0","1","1","0","0"]]

**Please don't chastise me for using mysql. I am dumping it as soon as this project is finished.

Upvotes: 0

Views: 267

Answers (1)

Benvorth
Benvorth

Reputation: 7722

The way you build your JOIN condition you will get results only from records with MONTH(contacts.lead_date) = MONTH(contacts.close_date).

To fix that and to get your actual Count I would suggest splitting the statement into two queries:

SELECT 
    a.monthnum, 
    a.lead_date_month, b.close_date_month, 
    b.close_date_month / a.lead_date_month
FROM
(
-- Count number of new leads
SELECT 
    months.monthnum as monthnum, 
    COUNT(*) as lead_date_month, 
    contacts.rep as rep 
FROM 
    months 
    LEFT JOIN contacts
ON 
    months.monthnum = MONTH(contacts.lead_date) 
    AND contacts.compid='$compid' 
    AND YEAR(contacts.lead_date) = '$year' 
    AND contacts.rep = '$rep' 
GROUP BY 
    months.monthnum 
ORDER BY 
    months.monthnum ASC
) as a
JOIN
(
-- Count number of closed leads
    SELECT 
    months.monthnum as monthnum, 
    COUNT(*) as close_date_month, 
    contacts.rep as rep 
FROM 
    months 
    LEFT JOIN contacts
ON 
    months.monthnum = MONTH(contacts.close_date) 
    AND contacts.compid='$compid' 
    AND YEAR(contacts.close_date) = '$year' 
    AND contacts.rep = '$rep' 
GROUP BY 
    months.monthnum 
ORDER BY 
    months.monthnum ASC
) as b
ON a.monthnum = b.monthnum

And MySQL isn't that bad. But MariaDB is better, you are right :-)

Upvotes: 1

Related Questions