-2

I'm attempting to join 3 tables, keeping the data from table 1 and 2, even if the data in table 3 is null. However, when joining the third table it changes the values fetched from table 2 and I cannot, for the life of me, figure out why!

Here's the SQL code that I know works:

select
segment,
count(distinct customers.customer_number) as customers,
round(count(distinct customers) * 100 / sum(count(distinct customers)) over (), 2) as percentage,
sum(amount) as revenue,
round(sum(amount) * 100 / SUM(SUM(amount)) over (), 2) as percentage,
sum(amount) / count(distinct customers.customer_number) as Revenue_Per_Customer
from customers
left join invoices on customers.customer_number=invoices.c_number
group by segment;

This returns the expected values.

However, when I join the third table it will change the values from the second table. The SQL code I add to the above, before the 'group by segment;' line is:

left join support_calls on customers.customer_number=support_calls.c_number

But the result changes the values from the second table.

I just want to add the values from the third table and not have them effect the rest of the table what so ever. Hopefully someone can figure out where I'm going wrong here.

  • [Why should I not upload images of code/data/errors when asking a question?](https://meta.stackoverflow.com/q/285551/3404097) [Why are images of text, code and mathematical expressions discouraged?](https://meta.stackexchange.com/q/320052/266284) [mre] [ask] [Help] – philipxy May 16 '22 at 18:25
  • What does "change the values from the second table" mean? There are 2 input tables to a join & 1 output table from a join & 1 from a select. Use enough words, sentences & references to parts of examples to clearly & fully say what you mean. – philipxy May 16 '22 at 18:27
  • 1
    Please in code questions give a [mre]--cut & paste & runnable code & example input; desired & actual output (including verbatim error messages); tags & versions; clear specification & explanation. For SQL include DDL & tabular initialization code. For debug that includes the least code you can give that is code that you show is OK extended by code that you show is not OK. When you get a result you don't expect, pause your overall goal, chop to the 1st subexpression with unexpected result & say what you expected & why, justified by documentation. PS `select *` for intermediate results. – philipxy May 16 '22 at 18:28
  • [Two SQL LEFT JOINS produce incorrect result](https://stackoverflow.com/q/12464037/3404097) – philipxy May 17 '22 at 01:06

0 Answers0