1
Dear, I have two tables, where Tabela1 has columns from A1 to A4, B1 to B4, C1 to C4 and D1 to D4, all of the decimal type (4,2) and with n rows; and the table2 has 3 columns being Total1, Total2 and Total3, where the sum of the fields of Tabela1 must be presented. Roughly I created the following precedent to update the table2:
DELIMITER $$
DROP PROCEDURE IF EXISTS teste$$
CREATE PROCEDURE teste()
BEGIN
update tabela2 set TOTAL1 = (select sum(A1) from tabela1) where coluna = 'A1';
update tabela2 set TOTAL1 = (select sum(B1) from tabela1) where coluna = 'B1';
update tabela2 set TOTAL1 = (select sum(C1) from tabela1) where coluna = 'C1';
update tabela2 set TOTAL1 = (select sum(A2) from tabela1) where coluna = 'A2';
update tabela2 set TOTAL1 = (select sum(B2) from tabela1) where coluna = 'B2';
update tabela2 set TOTAL1 = (select sum(C2) from tabela1) where coluna = 'C2';
update tabela2 set TOTAL1 = (select sum(A3) from tabela1) where coluna = 'A3';
update tabela2 set TOTAL1 = (select sum(B3) from tabela1) where coluna = 'B3';
update tabela2 set TOTAL1 = (select sum(C3) from tabela1) where coluna = 'C3';
update tabela2 set TOTAL1 = (select sum(A4) from tabela1) where coluna = 'A4';
update tabela2 set TOTAL1 = (select sum(B4) from tabela1) where coluna = 'B4';
update tabela2 set TOTAL1 = (select sum(C4) from tabela1) where coluna = 'C4';
update tabela2 set TOTAL2 = (select sum(A1) from tabela1) where coluna = 'A1';
update tabela2 set TOTAL2 = (select sum(B1) from tabela1) where coluna = 'B1';
update tabela2 set TOTAL2 = (select sum(C1) from tabela1) where coluna = 'C1';
update tabela2 set TOTAL2 = (select sum(A2) from tabela1) where coluna = 'A2';
update tabela2 set TOTAL2 = (select sum(B2) from tabela1) where coluna = 'B2';
update tabela2 set TOTAL2 = (select sum(C2) from tabela1) where coluna = 'C2';
update tabela2 set TOTAL2 = (select sum(A3) from tabela1) where coluna = 'A3';
update tabela2 set TOTAL2 = (select sum(B3) from tabela1) where coluna = 'B3';
update tabela2 set TOTAL2 = (select sum(C3) from tabela1) where coluna = 'C3';
update tabela2 set TOTAL2 = (select sum(A4) from tabela1) where coluna = 'A4';
update tabela2 set TOTAL2 = (select sum(B4) from tabela1) where coluna = 'B4';
update tabela2 set TOTAL2 = (select sum(C4) from tabela1) where coluna = 'C4';
update tabela2 set TOTAL3 = (select sum(A1) from tabela1) where coluna = 'A1';
update tabela2 set TOTAL3 = (select sum(B1) from tabela1) where coluna = 'B1';
update tabela2 set TOTAL3 = (select sum(C1) from tabela1) where coluna = 'C1';
update tabela2 set TOTAL3 = (select sum(A2) from tabela1) where coluna = 'A2';
update tabela2 set TOTAL3 = (select sum(B2) from tabela1) where coluna = 'B2';
update tabela2 set TOTAL3 = (select sum(C2) from tabela1) where coluna = 'C2';
update tabela2 set TOTAL3 = (select sum(A3) from tabela1) where coluna = 'A3';
update tabela2 set TOTAL3 = (select sum(B3) from tabela1) where coluna = 'B3';
update tabela2 set TOTAL3 = (select sum(C3) from tabela1) where coluna = 'C3';
update tabela2 set TOTAL3 = (select sum(A4) from tabela1) where coluna = 'A4';
update tabela2 set TOTAL3 = (select sum(B4) from tabela1) where coluna = 'B4';
update tabela2 set TOTAL3 = (select sum(C4) from tabela1) where coluna = 'C4';
END$$
DELIMITER ;
all teste();
However this type of repetitive code is not cool, I am trying to use the Precedent below, but the sum does not work for the variable str, follows code:
DELIMITER $$
DROP PROCEDURE IF EXISTS teste$$
CREATE PROCEDURE teste()
BEGIN
DECLARE x INT;
DECLARE str1 varchar(2);
DECLARE str2 varchar(2);
DECLARE str3 varchar(2);
SET x = 1;
WHILE x <= 4 DO
SET str1 = concat('A',x);
SET str2 = concat('B',x);
SET str3 = concat('C',x);
update tabela set TOTAL = (select sum(str1) from tabela2) where coluna = str1;
update tabela set TOTAL = (select sum(str2) from tabela2) where coluna = str2;
update tabela set TOTAL = (select sum(str3) from tabela2) where coluna = str3;
SET x = x + 1;
END WHILE;
END$$
DELIMITER ;
call teste();
I would like to know if there is the possibility of creating this looping.
Thank you.
The problem seems to me to be in
concat('A',x)
which does not return the reference to a column but the stringA1
for example.– Zuul
And the column "column"?
– thiagobarradas
column is the same column name, in this column I will have the values A1, A2, A3, A4, B1, B2...up to C4, outside this column I will have the column TOTAL, where the result of the sum of fields should be stored.
– Thiago
Actually, I believe the problem is in the Concat, but I couldn’t find a way to use the loop for the 4 variations of A,B,C. I couldn’t find a way to join the strings A,B,C with the variable x, so that the sum recognizes it as the name of a column.
– Thiago
@Thiago You need to make one
SELECT
within theconcat()
to get the value... I’m running out of time right now, but you can look at this Sqlfiddle, do what you need.– Zuul
Just to keep on understanding. In the first code you have
total1
,total2
andtotal3
. In the second onlytotal
. I understand you actually have a field, toocoluna
where it’s like spine A1 - total - sum(A1), spine A2 - total - sum(A2), and so on. That’s it?– thiagobarradas
I had already tried using select(Concat()), but sum does not recognize yet.
– Thiago