I have the following situation, I have a table where I am managing data of people, which I am saving by month and by type of insurer. The amount of data I'm saving for each person in a month can range from 1 to n . I have to place this data in a graph, so I need it to look like this:
January, February, March...December
Performing subquery I get them but it takes me a long time, the operations to perform. I have placed the indexes in the search and sum fields and even so it is very slow for me, it can take up to 15 minutes. In the table I am handling around 15,000 records. Then I leave the query to see if you can help me.
Thanks.
SELECT @var2 := tabla1.id_insured AS id_insured, @var1 := tabla1.pyear AS anio,
(SELECT SUM(tabla1.commission) FROM tabla1 WHERE tabla1.id_insured = @var2 AND tabla1.pyear = @var1 AND tabla1.month = '01') AS ene,
(SELECT SUM(tabla1.commission) FROM tabla1 WHERE tabla1.id_insured = @var2 AND tabla1.pyear = @var1 AND tabla1.month = '02') AS feb,
(SELECT SUM(tabla1.commission) FROM tabla1 WHERE tabla1.id_insured = @var2 AND tabla1.pyear = @var1 AND tabla1.month = '03') AS mar,
(SELECT SUM(tabla1.commission) FROM tabla1 WHERE tabla1.id_insured = @var2 AND tabla1.pyear = @var1 AND tabla1.month = '04') AS abr,
(SELECT SUM(tabla1.commission) FROM tabla1 WHERE tabla1.id_insured = @var2 AND tabla1.pyear = @var1 AND tabla1.month = '05') AS may,
(SELECT SUM(tabla1.commission) FROM tabla1 WHERE tabla1.id_insured = @var2 AND tabla1.pyear = @var1 AND tabla1.month = '06') AS jun,
(SELECT SUM(tabla1.commission) FROM tabla1 WHERE tabla1.id_insured = @var2 AND tabla1.pyear = @var1 AND tabla1.month = '07') AS jul,
(SELECT SUM(tabla1.commission) FROM tabla1 WHERE tabla1.id_insured = @var2 AND tabla1.pyear = @var1 AND tabla1.month = '08') AS ago,
(SELECT SUM(tabla1.commission) FROM tabla1 WHERE tabla1.id_insured = @var2 AND tabla1.pyear = @var1 AND tabla1.month = '09') AS sep,
(SELECT SUM(tabla1.commission) FROM tabla1 WHERE tabla1.id_insured = @var2 AND tabla1.pyear = @var1 AND tabla1.month = '10') AS octb,
(SELECT SUM(tabla1.commission) FROM tabla1 WHERE tabla1.id_insured = @var2 AND tabla1.pyear = @var1 AND tabla1.month = '11') AS nov,
(SELECT SUM(tabla1.commission) FROM tabla1 WHERE tabla1.id_insured = @var2 AND tabla1.pyear = @var1 AND tabla1.month = '12') AS dic
FROM tabla1
WHERE tabla1.pyear = '2018'
GROUP BY tabla1.id_insured
I think you are misreading the problem. As I understand you, what you are looking for is a table where you can see each insured and the sum of the commissions for each month of these, in 12 columns. A very simple way is using a conditional sum and grouping by the insured and the year (Eventually the year can go because you would be listing only one)
A while ago I posted this question and there was no better performance of my Query until I found this explanation in a forum so I leave it here in case someone else has the same problem. The process that is carried out is to join the subconsults with join. Here is the example: