Ajinkya Lotankar
Ajinkya Lotankar

Reputation: 61

Sum according to the combination of another table in sql

I have two tables:

Table 1 Items with sales

+-------+-------+
| Items | Sales |
+-------+-------+
| A     | 34    |
+-------+-------+
| B     | 22    |
+-------+-------+
| C     | 10    |
+-------+-------+

Table 2

Combination

+-------+-------+
| item1 | item2 |
+-------+-------+
| A     | B     |
+-------+-------+
| C     | B     |
+-------+-------+
| C     | A     |
+-------+-------+

I want sum of the combination Item1 and item2 value from table 1 e.g. A + B

Upvotes: 2

Views: 55

Answers (2)

Hemang A
Hemang A

Reputation: 1012

Please try this.

SELECT(A.Sales + B.Sales) AS Total, * FROM Combination
INNER JOIN ITEM A ON A.Items = C.Item1
INNER JOIN ITEM  B ON  B.Items = C.Item2

Upvotes: 2

Fahmi
Fahmi

Reputation: 37473

Join combination table with Item table twice

select b.sales+c.sales as totalval from combination a
inner join item b on a.item1=b.Items
inner join item c on a.item2=c.Items

Upvotes: 1

Related Questions