kazim
kazim

Reputation: 41

How do I join multible tables and SUM values?

I have the following tables (and example values):

**table1**
data_id (1, 2)

**table2:**
data_id (1, 2, 1, 2)
month(01, 01, 02, 02)
data(10, 5, 11, 2)

**table3**
data_id (1, 2, 1, 2)
month(01, 01, 02, 02)
data(10, 5, 11, 2)

**table4**
data_id (1, 2, 1, 2)
month(01, 01, 02, 02)
data(10, 5, 11, 2)

all table is connected by "data_id". i need to SUM the data field and GROUP BY month. table result looks like :

**result**
month(01, 02)
table2.data(15, 13)
table3.data(15, 13)
table4.data(15, 13)

What is the SQL statement that gets the result table?

Upvotes: 1

Views: 88

Answers (1)

Meherzad
Meherzad

Reputation: 8553

Try this query EDIT

SELECT t2.month, sum(t2.data), sum(t3.data), sum(t4.data)
FROM table1 t1, table2 t2, table3 t3, table4 t4
WHERE t1.data_id = t2.data_id AND t1.data_id = t3.data_id AND t1.data_id = t4.data_id
AND t2.month = t3.month AND t3.month = t4.month
GROUP BY t2.month;

It sums up value of data field month wise

FIDDLE

Upvotes: 2

Related Questions