0
I have the following separate tables, A and B which I am placing next to each other to facilitate:
+--------------+------------+-------------+
|ID Relacional | A | B |
+--------------+------------+-------------+
| 35 | 30 | 28 |
| 20 | 10 | 4 |
| 20 | 10 | 5 |
+--------------+------------+-------------+
I need to create a code that divides the value of B by the value of A.
Example: 1st line: 28/30.
But when the Relational ID is repeated, I need to divide only one value of A by the sum of the value of B.
Example: 2nd and 3rd row: 10 / (5 + 4)
My current code:
select
sum(A.valorA) as ColunaA,
sum(B.valorB) as ColunaB,
sum(B.valorB) /sum(A.valorA) as Resultado
from tableA A, tableB B where A.id = B.id
group by ...
order by ...
The resulting table is
+--------------+------------+-------------+
| ColunaA | ColunaB | Resultado |
+--------------+------------+-------------+
| 30 | 28 | 0.93 |
| 10 | 4 | 0.45 (9/20) |
| 10 | 5 | |
+--------------+------------+-------------+