0
I’m trying to do an SQL search using Join between two tables. The first table is a table containing monthly savings values for a given project. Each time someone registers a project on the site, a PHP routine generates 12 values, one in the current month and another 11 months forward. Since the website has a feature where the factory manager can choose the period to show savings, I created a table containing only the months in the period the manager wants to view. I would like to create an SQL query that based on the calendar table, serves as the basis for the table containing the savings and actual dates of the implemented actions.
In other words, for each month of the calendar table, I would like to get the sum of the share table of the respective month (sum all actions in that month of the share table) from the monthly Saving_column and return all months of the interval defined in the calendar table. In the calendar table months that there are no Savings, I would like to return the value 0.
The calendar table has the following structure
The table with the 12 stock values has the following structure
The research I’m trying to execute is: SELECT date_format(calendar.Data, '%Y-%m') as Mes, sum(action_plan_detail.Saving_Mensal) AS Saving FROM calendar LEFT JOIN action_plan_detail ON action_plan_detail.Saving_Mensal = calendar.Saving_Mensal GROUP BY Mes
and the return is zero in all months
Can someone help me determine which correct research to be done here?
The result I would like in the end would be
Mes | Saving_Mensal
--------|--------------
01-2018 | 20000
02-2018 | 20000
03-2018 | 20000
04-2018 | 20000
05-2018 | 20000
06-2018 | 0 * não existe nenhum lançamento na tabela de açoes com valores neste mês
07-2018 | 0
08-2018 | 0
09-2018 | 20000
10-2018 | 20000
11-2018 | 20000
12-2018 | 20000
I understood nothing, I recommend trying to simplify your question and focus more on the problem, not the context.
– Francisco
For each existing month in the calendar table, sum all equivalent months in the stock table (add monthly Saving_for all records with equivalent month of the calendar table) and return the total monthly Saving_value in that month, for all existing months in the calendar table.
– Fernando Ferrari Fernandes