運行此查詢時,COUNT函數不會按'check_if_new_customer'標誌進行過濾。我在這篇文章中讀到:http://dev.mysql.com/tech-resources/articles/wizard/page4.html在某些情況下SUM可以用來代替COUNT來獲得更準確的結果,但是當我嘗試這樣做時,我得到了非常不同的東西,它似乎顯示出數量翻倍。我想這可能是因爲我總結了id字段中的UUID,而不是在那個點上計數。關於我可以在那裏計算所有現有客戶數量與新客戶數量的任何建議?智能統計連接mysql中的id
SELECT
YEAR(so.date_entered),
so.technical_address_country,
so.technical_address_state,
COUNT(so.id) as all_sales,
COUNT(mf.id) as all_jobs,
SUM(so.total_value) as all_value,
COUNT(IF(so.check_if_new_customer=1,so.id,0)) as sales_order_new,
SUM(IF(so.check_if_new_customer = 1,so.total_value,0)) as total_value_new,
COUNT(IF(so.check_if_new_customer=1,mf.id,0)) as jobs_new,
COUNT(IF(so.check_if_new_customer=0,so.id,0)) as sales_order_existing,
SUM(IF(so.check_if_new_customer = 0,so.total_value,0)) as total_value_existing,
COUNT(IF(so.check_if_new_customer=0,mf.id,0)) as jobs_existing,
SUM(IF(so.check_if_new_customer=0,mf.id,0)) as jobs_existing_t
FROM
sugarcrm2.so_order so
LEFT JOIN
sugarcrm2.mf_job mf on so.id = mf.sales_order_id
WHERE
so.date_entered > "2011-10-30" AND
so.technical_address_country IS NOT NULL AND
so.technical_address_state IS NOT NULL AND
so.deleted = 0 AND
so.has_been_promoted = 1
GROUP BY
YEAR(so.date_entered),
so.technical_address_country,
so.technical_address_state
ORDER BY
so.technical_address_country, so.technical_address_state