Can you explaing what the problem is? We can't see your row numbers, so we don't know what is in C10, D4 or D5
Also, is this really a SQL problem?
Sorry, i'm just using the excel to draw two table to get the result:-
I still have not idea what the problem is
you will need to provide something like the following and why you are selecting two values from cats and only one value from dog and what the relationship is between the two
create table #meow(year int, value1 int, value2 int, value3 int) insert into #meow select 2017, 52753,22995,100 union select 2018, 52754,22996,300 union select 2018, 52755,22996,200 create table #woof(year int, value1 int, value2 int, value3 int) insert into #woof select 2017, null,22995,300 union select 2018, null,22996,400 ;with cte as ( select year, value2, sum(value3) as sumkitty from #meow d where value2 = 22996 and year = 2018 group by year, value2 ) select w.year, c.value2, c.value2 , (w.value3 + c.sumkitty ) From cte c join #woof w on c.value2 = w.value2 --group by w.year, c.value2 drop table #meow drop table #woof
I would like to know the total amount from table "cat" & table "dog" from which value ="22996"
which is 300 + 200 + 400 , total = 900
and why is 52754 selected in your final result and not 52755?