Cursor stored Mysql precedent

Asked

Viewed 226 times

2

I am inside a cursor that has 10 Rows of result. 2 Rows percentem is a number. example: 1,2 = 1, 3,4 = 2, 5,6 = 6, 7,8 = 9 , 9,10 = 3.

How do I add only the values that correspond to the same number inside the cursor?

This is my FETCH from the cursor, I have 10 Rows inside it, the spv_total_item is from a specific spv_moeda_id and I want to add when the spv_moeda_id are equal, how do I do this?

FETCH cursor_comissao INTO SPV_COMISSAO_ID, SPV_COMPRA_ID, SPV_PERCENTUAL, SPV_MOEDA_ID, SPV_DATA_CAMBIO, SPV_TOTAL_ITEM;

inserir a descrição da imagem aqui

I would be very pleased if someone would help me with this logic.

I tried, but the first time it’s gonna run Row, the spv_moeda_check is null, and only at the end of the loop will it have a value..

 OPEN cursor_comissao;


-- INICIANDO A VARIAVEL DURAÇÃO DO LOOP PARA 0;
SET SPV_DURACAO = 0;

loop_comissoes: LOOP

FETCH cursor_comissao INTO SPV_COMISSAO_ID, SPV_COMPRA_ID, SPV_PERCENTUAL, SPV_MOEDA_ID, SPV_DATA_CAMBIO, SPV_TOTAL_ITEM;    


  IF CURSOR_FIM = 1 THEN 
        LEAVE loop_comissoes;
    END IF; 

    IF SPV_DURACAO = SPV_LOOP THEN
       SET CURSOR_FIM = 1;
       LEAVE loop_comissoes;
    END IF;

    IF SPV_COMISSAO_ID = SPV_ID_CHECK THEN
        SPV_TOTAL_ITEM = SPV_TOTAL_ITEM + SPV_TOTAL_ITEM;
        ITERATE loop_comissoes;
    END IF;



    CALL SP_CONVERTE_REAIS_DATA(SPV_TOTAL_ITEM,SPV_MOEDA_ID,SPV_DATA_CAMBIO,SPV_TOTAL_ITEM);

-- SETANDO O CAMPO VALOR DA ROW ATUAL DO LOOP;
    UPDATE cliente_compra_comissao SET valor = SPV_TOTAL_ITEM WHERE id = SPV_COMISSAO_ID;       

    SET SPV_DURACAO = SPV_DURACAO + 1;
    SET SPV_TOTAL_ITEM = 0;
    SET SPV_ID_CHECK = SPV_COMISSAO_ID;
END LOOP loop_comissoes;
CLOSE cursor_comissao;
CLOSE cursor_comissao_id;
  • Which bank is this?

  • opa sorry, phpMyAdmin

  • Add the mysql tag to the question :)

  • tag added, rs

  • I am confused. What is the relation of the attached table, with the command of FETCH?

  • This table is the values that will enter into each variable of the FETCH, in the same order, I put to understand what I need, my cursor will rotate each line of this result, but what I need is that when the id of each line is equal, it adds the values. It would be more or less something like, I need it to store the id value of the previous line to check on the next line if it is equal, if it is sum, if it does not store the new value and back to start.. something like..

  • Now I see the logic.

  • I’ve been trying to make sense of it for three hours, but I can’t. I did something here, but the problem is that I can’t store the first time of the coin, so it’s only if, because the first time I run Row, the spv_moeda_check will be null;

Show 3 more comments
No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.