Add and split columns to oracle

Asked

Viewed 996 times

-2

I have 4 columns. I want to do the subtraction process in 3 and divide by the fourth.

How do I do this operation in BD Oracle?

COLUNA1 - COLUNA2 - COLUNA3 = RESULTADO;

RESULTADO / COLUNA4 = COLUNA5
  • Simple (COLUNA1 - COLUNA2 - COLUNA3) / COLUNA4. If you want the column of sums do select COLUNA1 - COLUNA2 - COLUNA3, (COLUNA1 - COLUNA2 - COLUNA3) / COLUNA4

1 answer

0

If you want to return the result directly in the sql query the syntax would be as follows:

select (COLUNA1-COLUNA2-COLUNA3)/COLUNA4 AS Resultado 
  from SUATABELA

If necessary perform the operation in a Pl/SQL block/procedure the syntax would be

declare
  vColuna1 number;
  vColuna2 number;
  vColuna3 number;
  vColuna4 number;
  vResultado number;
begin
  select COLUNA1, COLUNA2, COLUNA3, COLUNA4 into vColuna1, vColuna2, vColuna3, vColuna4
    from SUATABELA
   where ID = :CHAVEPK;

  vResultado := (vColuna1 - vColuna2 - vColuna3) / vColuna4;
  dbms_output.put_line(vResultado);
end;
  • 1

    Be careful if column 4 is zero , it will give an error divided by zero , make (case when column 4=0 then null Else (col...) end)

Browser other questions tagged

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