SELECT
deals.id, products.name as product, deals_tmp.geo, departments.name as department, DATE(deals.created_at), deals.usd_summary, deals_tmp.ad_url, deals_tmp.ad_places, deals_tmp.results, deals_tmp.wallet_type as payment_method, deals_tmp.extention, deals_tmp.cost_item, deals_tmp.about,
CASE
WHEN deals_tmp.id IS NULL THEN "CRM"
ELSE "GOOGLE DOCS"
END
FROM `deals`
LEFT JOIN `deals_tmp` ON `deals`.`id` = `deals_tmp`.`deal_id`
LEFT JOIN `departments` ON `deals`.`department_id` = `departments`.`id`
LEFT JOIN `partners` ON `partners`.`id` = `deals`.`partner_id` LEFT JOIN `products` ON `products`.`id` = `partners`.`product_id`
WHERE `deals`.`created_at` BETWEEN '2019-08-01 00:00:00.000000' AND '2019-08-31 23:59:59.000000' AND `deals`.`status` = 'active'
ORDER BY `deals`.`id` DESC