Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8944839
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T12:06:38+00:00 2026-06-15T12:06:38+00:00

I have some joins that are multiplying my results in a select statement like

  • 0

I have some joins that are multiplying my results in a select statement like the following:

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

If I do this query, I get the same id twice.

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

How sum the total_value, but only if the id is distinct?

I’m looking to solved the above problem so that I can eliminate duplication in the following query:

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 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-15T12:06:39+00:00Added an answer on June 15, 2026 at 12:06 pm

    so, why are you left joining?

    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
    

    EDIT: well, in that case, this works:

    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
    

    last:

    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
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have following statement for query articles from some sections Article.all(:joins => :sections, :conditions
I have a view with some joins in it. I'm doing a select from
I have a query that make some joins. SELECT feedback.note as notes, to_char(feedback.data_compilazione,'DD/MM/YYYY') as
I have some text lines like that : vt_wildshade2^508^508 vt_ailleurs2^1188^1188 ... vt_high2^13652^13652 Is it
I have some data in an ArrayList and I would like to use that
After doing some joins and stuff I have table in the following format name
I have a query that JOINs some tables to get a list of products
I have some SQL that gets a few details about a table. SELECT Column_Name,
I have some what of a challenging delete statement that I need help with.
I have some sophisticated query (just select with many nested joins and subqueries). I've

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.