我有一些连接将我的结果乘以 select 语句,如下所示:
SELECT
SUM(so.total_value)
FROM
sugarcrm2.so_order so
LEFT JOIN
sugarcrm2.mf_job mf on so.id = mf.sales_order_id
LEFT JOIN
reporting.backblaze_sales_orders bb on so.id = bb.id
LEFT JOIN
reporting.territories_copy_copy tc on TRIM(so.technical_address_state) = tc.state
WHERE
so.id = "106f3ff1-56df-8d66-3f9b-4fec87aa12fd"
ORDER BY
id
如果我执行此查询,我会两次获得相同的 id。
SELECT
so.id
FROM
sugarcrm2.so_order so
LEFT JOIN
sugarcrm2.mf_job mf on so.id = mf.sales_order_id
LEFT JOIN
reporting.backblaze_sales_orders bb on so.id = bb.id
LEFT JOIN
reporting.territories_copy_copy tc on TRIM(so.technical_address_state) = tc.state
ORDER BY
id
如何对 total_value 求和,但前提是 id 不同?
我正在寻求解决上述问题,以便可以消除以下查询中的重复:
SELECT
IF(so.technical_address_country = 'USA' OR so.technical_address_country = 'Canada',1,0) as home,
IF(so.technical_address_country = 'USA' OR so.technical_address_country = 'Canada', tc.territory, null) as territory,
so.technical_address_country,
IF(so.technical_address_country = 'USA' OR so.technical_address_country = 'Canada', TRIM(so.technical_address_state), null) as state,
/* ALL JOBs */
COUNT(distinct so.id) as all_sales,
COUNT(mf.id) as all_jobs,
SUM(so.total_value) as all_value,
count(distinct case when so.check_if_new_customer=1 then so.id else null end) as sales_order_new,
SUM(IF(so.check_if_new_customer=1 AND mf.id IS NOT NULL,1,0)) as jobs_new,
SUM(IF(so.check_if_new_customer=1,so.total_value,0)) as total_value_new,
count(distinct case when so.check_if_new_customer=0 then so.id else null end) as sales_order_existing,
SUM(IF(so.check_if_new_customer=0 AND mf.id IS NOT NULL,1,0)) as jobs_existing,
SUM(IF(so.check_if_new_customer = 0,so.total_value,0)) as total_value_existing,
/* BACKBLAZES */
count(distinct case when bb.id is not null then so.id else null end) as all_sales_back_blaze,
SUM(IF(bb.id IS NOT NULL and mf.id is not null,1,0)) as all_jobs_back_blaze,
SUM(IF(bb.id IS NOT NULL,so.total_value, 0)) as all_value_back_blaze,
count(distinct case when bb.id is not null AND so.check_if_new_customer = 1 then so.id else null end) as sales_order_new_back_blaze,
SUM(IF(so.check_if_new_customer=1 AND mf.id IS NOT NULL AND bb.id IS NOT NULL,1,0)) as jobs_new_back_blaze,
SUM(IF(so.check_if_new_customer = 1 AND bb.id IS NOT NULL,so.total_value,0)) as total_value_new_back_blaze,
count(distinct case when bb.id is not null AND so.check_if_new_customer = 0 then so.id else null end) as sales_order_existing_back_blaze,
SUM(IF(so.check_if_new_customer=0 AND mf.id IS NOT NULL AND bb.id IS NOT NULL,1,0)) as jobs_existing_back_blaze,
SUM(IF(so.check_if_new_customer = 0 AND bb.id IS NOT NULL,so.total_value,0)) as total_value_existing_back_blaze,
/* NOT BACKBLAZES */
SUM(IF(bb.id IS NULL,1,0)) as all_sales_non_back_blaze,
SUM(IF(bb.id IS NULL and mf.id is not null,1,0)) as all_jobs_non_back_blaze,
SUM(IF(bb.id IS NULL,so.total_value, 0)) as all_value_non_back_blaze,
SUM(IF(so.check_if_new_customer=1 AND bb.id IS NULL ,1,0)) as sales_order_new_non_back_blaze,
SUM(IF(so.check_if_new_customer=1 AND mf.id IS NULL AND bb.id IS NOT NULL,1,0)) as jobs_new_non_back_blaze,
SUM(IF(so.check_if_new_customer = 1 AND bb.id IS NULL,so.total_value,0)) as total_value_new_non_back_blaze,
SUM(IF(so.check_if_new_customer=0 AND bb.id IS NULL,1,0)) as sales_order_existing_non_back_blaze,
SUM(IF(so.check_if_new_customer=0 AND mf.id IS NULL AND bb.id IS NULL,1,0)) as jobs_existing_non_back_blaze,
SUM(IF(so.check_if_new_customer = 0 AND bb.id IS NULL,so.total_value,0)) as total_value_existing_non_back_blaze
FROM
sugarcrm2.so_order so
LEFT JOIN
sugarcrm2.mf_job mf on so.id = mf.sales_order_id
LEFT JOIN
reporting.backblaze_sales_orders bb on so.id = bb.id
LEFT JOIN
reporting.territories_copy_copy tc on TRIM(so.technical_address_state) = tc.state
WHERE
so.date_entered >= "2011-10-30" AND so.date_entered <="2012-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 AND
mf.deleted = 0
GROUP BY
UPPER(TRIM(so.technical_address_country)),
UPPER(TRIM(state))
ORDER BY
home DESC,
so.technical_address_country ASC,
state ASC