I try to regroup data (with SQL) by months and make a subtraction (with groups by months).
TABLE 1 :
| DATE | MONEY1. |
|---|---|
| 01/01/2023 | 30 |
| 10/01/2023 | 5 |
| 12/02/2023 | 30 |
| 18/02/2023 | 20 |
TABLE 2 :
| DATE | MONEY2. |
|---|---|
| 04/01/2023 | 5 |
| 8/01/2023 | 10 |
| 19/02/2023 | 5 |
| 20/02/2023 | 5 |
And I want to get this as my result:
| MONTH | MONEY1. | MONEY2. | MONEY1-MONEY2 |
|---|---|---|---|
| January | 35 | 15 | 20 |
| February | 50 | 10 | 40 |
Thank you for your help !
I can group by month each table (in separate SELECT commands), but I can't make two operations in same table.
You can use
group byand appropriate function to extract month from your date (in the example oracle dialect is being used) to sum money in your tables. And the join those aggregated results:Demo can be seen here.
For BASE query would look something like this: