I have table like following
job_id | quantity |
---|---|
2948 | 0 |
2947 | 3 |
2949 | 34 |
2950 | 4 |
And I have another table like following
job_id | quantity |
---|---|
1 | 10 |
2949 | 10 |
2949 | 20 |
If I run SELECT job_id, sum(quantity) FROM arr_database.test_table group by job_id;
job_id | quantity |
---|---|
1 | 10 |
2949 | 30 |
As we can see , job_id
is common in both table , I want to deduct sum of quantity which is available in second table from first table .
So the output will be like following as sum of quantity for job_id 2949 is 30 , and I dedcut that 30 from table one where job id is 2949 .
job_id | quantity |
---|---|
2948 | 0 |
2947 | 3 |
2949 | 4 |
2950 | 4 |
2
Answers
If the job_id is unique in the first table then you can simply do it using this arithmitic
max()
–sum()
:Demo here
If it is not unique then do it as follows :
Demo here