이 쿼리를 실행하면 COUNT 함수가 'check_if_new_customer'플래그로 필터링하지 않습니다. 나는이 기사에서 읽었습니다 : http://dev.mysql.com/tech-resources/articles/wizard/page4.html SUM은 어떤 경우에는 COUNT 대신에 더 정확한 결과를 얻기 위해 사용될 수 있지만, 시도 할 때 매우 다른 것을 얻습니다. 이는 숫자가 엄청나게 두 배로 늘어나는 것처럼 보입니다. 나는 그 시점에서 계산하는 대신 id 필드에있는 UUID를 합산하기 때문에 이것이 가능할 것이라고 생각합니다. 기존 고객 전체와 신규 고객을 모두 계산할 수있는 방법에 대한 제안은 무엇입니까?join 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