I have 2 tables below.I want to join them
Table1
--------------
T1_id | desc
--------------
1 | test 1
2 | test 2
table2 (detail)
---------------
T2_id | Price
----------------
1 | 100000
1 | 0
1 | 0
2 | 300000
2 | 0
2 | 0
2 | 0
i want the results for this
--------------
code | total
--------------
1 | 100000
2 | 300000
this query
select a.T1_id as Code,
b.price as Total
from Table1 a
inner join table2 b on b.T2_id = a.T1_id
group by a.T1_id ,b.price;
CodePudding user response:
Use below query instead to get the total price
select a.T1_id as Code,
SUM(b.price) as Total
from Table1 a
inner join table2 b on b.T2_id = a.T1_id group by a.T1_id;
Sample output:
CodePudding user response:
Referring to the sample
To exclude 0 values:
SELECT T1_ID AS CODE, PRICE AS PRICE FROM TABLE1 JOIN TABLE2 ON T1_ID=T2_ID
WHERE PRICE<>0
sum of same id:
SELECT T1_ID AS CODE, SUM(PRICE) AS PRICE FROM TABLE1 JOIN TABLE2 ON T1_ID=T2_ID
GROUP BY T1_ID