Aggregate data from 2 tables

时间:2018-02-05 12:59:51

标签: mysql sql aggregate

I want to aggregate data from table A and B into one output. Problem I am facing is that some single records are needed from table A (where is some ID in a column connected with table B containing multiple records with the same ID). I want to data in one column in table B and add this value to related ID from table A.

I have already tried with joins but it is not working for me still. Can you please take a look at this code?

select r.variable, s.variable_1  s.variable_2, sum(r.sum), 
from table1 r
join table2 s on r.variable = s.variable
where some_cirrcumstances
group by  r.variable ,s.variable_1
order by r.variable ,s.variable_1;

Regards

Edit: please keep an eye on this translation

Please find an example what results I want:

Data in table A:

ID | variable_1 | variable_2 | Description

There is a lot of unique rows and I want to combine it with data from table B which looks like:

Data in table B:

Week_1 | ID | other_variable_1 | our_variable Week_1 | ID | other_variable_1 | our_variable Week_1 | ID | other_variable_1 | our_variable

ID is a connector between but I dont know how to combine these data. We can have multiple rows for one ID and sum of column per ID is needed.

0 个答案:

没有答案