1
I’m having doubts about how to query. The problem is the following:
- I have two tables similar to the ones described below (I don’t have exactly how they were, but are next to the representation below):
Table 1 - Countries
| País | Empresa |
|País A| Empresa 1|
|País A| Empresa 2|
|País A| Empresa 3|
|País B| Empresa 4|
|País B| Empresa 5|
|País C| Empresa 6|
Table 2 - Sales
| Código | Vendedor | Comprador | Valor |
| 1 | Empresa 1| Empresa 5 | 100 |
| 2 | Empresa 1| Empresa 4 | 20 |
| 3 | Empresa 6| Empresa 2 | 120 |
| 4 | Empresa 5| Empresa 6 | 10 |
| 5 | Empresa 6| Empresa 1 | 200 |
| 6 | Empresa 4| Empresa 2 | 60 |
In table 1 there is no key Primary. In table 2 the column "code" is key Primary. All table columns are varchar, except the columns "code" and "value".
The result should be a table with the purchase and sale values of each country. Of the type below:
Table 3 - Result
| País | Vendas | Compras |
| País A | 120 | 280 |
| País B | 70 | 120 |
| País C | 220 | 10 |
I tried to write a query that arrived at this result, but I couldn’t. In the querys that I did, the closest I could was the list of countries, but the sum was not correct. Could someone help me with this problem? Please.
But your solution is identical to the one I suggested 5 minutes earlier...
– Sorack
Sorry friend, had not even visualized its solution, was creating the example in fiddle
– Marciano Machado