只有当我有一个不同的ID时,我该如何求和?

时间:2012-12-03 17:56:23

标签: mysql group-by sum

我有一些连接将我的结果乘以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

1 个答案:

答案 0 :(得分:2)

那么,你为什么要离开?

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

编辑:嗯,在这种情况下,这有效:

SELECT *, subq.sumfield                -- or fewer fields
FROM 
  sugarcrm2.so_order so 
-- stuff
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
-- stuff end
LEFT JOIN (
  SELECT id, SUM(total_value) as sumfield 
  FROM sugarcrm2 
  -- joins?   I think not, but you should know
  GROUP BY id
  ) as subq on so.id = subq.id
WHERE 
  so.id = "106f3ff1-56df-8d66-3f9b-4fec87aa12fd"
ORDER BY 
 so.id

最后:

SELECT * FROM

--  this subquery is **only** sugarcrm2 based.
(SELECT 
  technical_address_country                 as oby
  UPPER(TRIM(so.technical_address_country)) as F1,
  UPPER(TRIM(state))                        as F2,

  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,
  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,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,so.total_value,0)) as total_value_existing,
FROM 
  sugarcrm2.so_order so 
LEFT JOIN    -- check this please for duplicating records
  reporting.territories_copy_copy tc on TRIM(so.technical_address_state) = tc.state
GROUP BY 
  UPPER(TRIM(so.technical_address_country)),   --  trim?? upper??  can they be without functions?
  UPPER(TRIM(state))
) AS Q1 LEFT JOIN 

--  To be sure about this subquery is necesary to understud the schema.  it is a copy of your original query.
(SELECT
  UPPER(TRIM(so.technical_address_country)) as F1,
  UPPER(TRIM(state))                        as F2,

  COUNT(mf.id) as all_jobs,
    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=0 AND mf.id IS NOT NULL,1,0)) as jobs_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 
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))
) AS Q2 on Q1.F1 = Q2.F1 and Q1.F2 = Q2.F2

ORDER BY 
  home  DESC,
  oby   ASC,   -- oby is similar but not F2, you can change
  F2    ASC