See the SQL query below, it count the number of Yes and No between the CheckDate
On the MatchSales field – it do not work how I wanted to be. It should find D.MobileNO (depending on the CheckDate) match to S.mobile (Check all record in the sales table)
SELECT D.Username,
SUM(CASE WHEN D.type = 'Yes' AND UNIX_TIMESTAMP(CheckDate) >= $From_timestamp AND UNIX_TIMESTAMP(CheckDate) <= $To_timestamp THEN 1 ELSE 0 END) as Yes,
SUM(CASE WHEN D.type = 'No' AND UNIX_TIMESTAMP(CheckDate) >= $From_timestamp AND UNIX_TIMESTAMP(CheckDate) <= $To_timestamp THEN 1 ELSE 0 END) as No,
SUM(CASE WHEN S.mobile IS NULL THEN 0 ELSE 1 END) as MatchSales
FROM dairy as D
LEFT JOIN (SELECT DISTINCT mobile FROM sales) as S on D.MobileNo = S.mobile
WHERE source = 'Company'
GROUP BY D.Username
simply add the Checkdate condition from your
CASEto thewhereclause:without it you will full scan dairy