Transfer records from one table to another by adding subtotal

0

How can I transfer this Source_table that has D (must) records:

ID  MOV IMPORTE CLASE
1   1   100.00  D
2   1   200.00  D
3   1   50.00   D
4   2   450.00  D
5   2   80.00   D
6   3   30.00   D
7   3   50.00   D
8   3   70.00   D

And that in the new Target_table for each Mov insert a total, in this way:

ID  MOV IMPORTE CLASE
1   1   100.00  D
2   1   200.00  D
3   1   50.00   D
4   1   350.00  H
5   2   450.00  D
6   2   80.00   D
7   2   530.00  H
8   3   30.00   D
9   3   50.00   D
10  3   70.00   D
11  3   150.00  H

I am preparing a MySQL table and as I see it, I may have to do several steps.

    
asked by Piropeator 05.05.2017 в 18:58
source

3 answers

2

I think you could do it in two steps ...

First, copy the data from the source table to the target table:

insert into Tabla_destino (ID, MOV, IMPORTE, CLASE)
select ID, MOV, IMPORTE, CLASE from Tabla_origen;

Then, calculate the subtotals and insert them into the destination table:

insert into Tabla_destino (ID, MOV, IMPORTE, CLASE)
select @row := @row + 1 as ID, MOV, sum(IMPORTE) as IMPORTE, 'H' as CLASE
from Tabla_destino as D, (SELECT @row := (select MAX(ID)+1 from Tabla_origen)) as R
group by MOV;

If the column ID is self-enumerated, then the query is much simpler, you do not need the variable @row .

    
answered by 05.05.2017 / 19:10
source
1

One of the advantages of the database is that they do not have to be added in an orderly manner, so you show them in that way.

You can simply copy all the data INSERT INTO tabla_Destino SELECT * FROM tabla_Origen

and then with a stored procedure send a cursor for each

DELIMITER $$

CREATE PROCEDURE insertar_total()
BEGIN

 DECLARE v_terminado INTEGER DEFAULT 0;
 DECLARE v_mov INTEGER DEFAULT 0;

 DEClARE mov_cursor CURSOR FOR 
 SELECT mov FROM tabla_origen;

 DECLARE CONTINUE HANDLER 
        FOR NOT FOUND SET v_terminado = 1;

 OPEN mov_cursor;

 WHILE terminado = 0 DO

 FETCH mov_cursor INTO v_mov;
 INSERT INTO tabla_destino VALUES (NULL, v_mov, (SELECT SUM(importE) FROM tabla_origen WHERE mov = v_mov), 'H' );


 END WHILE;

 CLOSE email_cursor;

END$$

DELIMITER ;

Then send to call your procedure CALL insertar_total();

And you order them so that the total appears below each movement

SELECT * FROM tabla_destino ORDER BY mov ASC, clase ASC;

EDITO, maybe a simpler way would be

INSERT INTO tabla destino
SELECT NULL, mov, SUM(importe), 'h',  FROM tabla_Origen GROUP BY mov;

And just like before you would only stay when you call her order

SELECT * FROM tabla_Destino ORDER BY mov ASC, clase ASC

    
answered by 05.05.2017 в 20:11
1

UPDATE

If the value of the column ID in the table TABLA_DESTINO is not self-generated, then it can be done:

INSERT INTO TABLA_DESTINO (ID, MOV, IMPORTE, CLASE)
SELECT
    @id := @id + 1,
    t.MOV,
    t.IMPORTE,
    t.CLASE
FROM
    (
    SELECT
        ID,
        MOV,
        IMPORTE,
        CLASE
    FROM
        TABLA_ORIGEN
    UNION ALL
        SELECT
        1,
        MOV,
        SUM(IMPORTE),
        'H'
    FROM
        TABLA_ORIGEN
    GROUP BY
        MOV
    ) AS t,
    (
    SELECT @id := 0 FROM DUAL
    ) AS i
ORDER BY t.MOV, t.CLASE, t.ID

Assuming that the column ID in the table TABLA_DESTINO has the attribute AUTO_INCREMENT you can use the following command:

INSERT INTO TABLA_DESTINO (MOV, IMPORTE, CLASE)
SELECT
    t.MOV,
    t.IMPORTE,
    t.CLASE
FROM (
    SELECT
        ID,
        MOV,
        IMPORTE,
        CLASE
    FROM
        TABLA_ORIGEN
    UNION ALL
        SELECT
        1,
        MOV,
        SUM(IMPORTE),
        'H'
    FROM
        TABLA_ORIGEN
    GROUP BY
        MOV
) AS t
ORDER BY t.MOV, t.CLASE, t.ID
    
answered by 05.05.2017 в 21:02