Foros Club Delphi

Foros Club Delphi (https://www.clubdelphi.com/foros/index.php)
-   Firebird e Interbase (https://www.clubdelphi.com/foros/forumdisplay.php?f=19)
-   -   duda: aritmetica en procedimiento en Firebird (https://www.clubdelphi.com/foros/showthread.php?t=96079)

novato_erick 26-01-2023 17:46:57

duda: aritmetica en procedimiento en Firebird
 
Hola Chicos cómo esta?

Estoy realizando algunas consultas la cual me muestra total de ventas de la tabla FACTURA_VENTAS por mes la cual no tengo ningún problema sin embargo me solicitaron que mostrara un campo la cual muestre el crecimiento tanto en monto como en porcentaje.

Mi duda es cual sería la mejor manera aprovechando el procedimiento almacenado de total de ventas por mes para traer información cómo por ejemplo:

Código:

se obtiene de:                  (MAC - MA)                (MC / M) * 100
MES              MONTO      MONTO_CRECIMIENTO    CRECIMIENTO_PORC
ENERO        27457.00          27457.00                    100%
FEBRERO    28987.57            1500.57                    5.18%
MARZO      30211.21            1223.67                    4.05%

MAC= MES ACTUAL
MA= MES ANTERIOR
MC= MONTO CRECIMIENTO
M= MONTO

¿Será posible efectuar este tipo de Artimetica en Firebird o recomiendan alguna otra solución?

Aquí muestro parte de mi procedimiento
Código SQL [-]
CREATE PROCEDURE VENTAS_CRECE_POR_MESCONTADO(
  ANIO SMALLINT)
RETURNS(
  NOMBREMES VARCHAR(25),
  MONTO_VENTAS TYPE OF COLUMN FACTURAS_VENTAS.MONTOTOTAL,
  CRECIMIENTO NUMERIC(12, 2))
AS
DECLARE VARIABLE LNI SMALLINT;
DECLARE VARIABLE MONTO_TOTALNC NUMERIC(12, 2);
DECLARE VARIABLE MONTO_CRECIMIENTO NUMERIC(12, 2);
BEGIN
   MONTO_CRECIMIENTO = 0;
   CRECIMIENTO = 0;  
   lnI = 1;
   WHILE (lnI <= 12) DO
   BEGIN
      NOMBREMES = DECODE(lnI,  1, 'ENE',  2, 'FEB',  3, 'MAR',  4, 'ABR',  5, 'MAY',  6, 'JUN',
                                  7, 'JUL',  8, 'AGO',  9, 'SEP', 10, 'OCT', 11, 'NOV', 12, 'DIC');
      MONTO_VENTAS = (SELECT
                     SUM(FACTURAS_VENTAS.MONTOTOTAL)
                  FROM FACTURAS_VENTAS
                  WHERE EXTRACT(MONTH FROM FACTURAS_VENTAS.FECHA) = :lnI AND
                     EXTRACT(YEAR FROM FACTURAS_VENTAS.FECHA) = :ANIO);
      MONTO_TOTALNC = (select coalesce(SUM(NOTAS_CREDITOS.IMPORTE_TOTAL), 0)
                  FROM NOTAS_CREDITOS                   
                  Where  EXTRACT(MONTH FROM NOTAS_CREDITOS.FECHA_ACTUAL) = :lnI AND
                  EXTRACT(YEAR FROM NOTAS_CREDITOS.FECHA_ACTUAL) = :ANIO);                         
      MONTO_VENTAS = COALESCE(MONTO_VENTAS, 0.00);     
      MONTO_VENTAS = (MONTO_VENTAS - COALESCE(MONTO_TOTALNC, 0.00));
      /*Los expertos pueden colaborarme con qP:-) ya que al hacer el recorrido no es posible tomar el mes actual para ser restado al mes anterior y así sucesivamente:*/
      SUSPEND;                
      lnI = lnI + 1;
   END

 END;

Saludos;

novatoerick.

duilioisola 26-01-2023 19:35:30

Yo lo haría así:

Código SQL [-]
create procedure ventas_crece_por_mescontado (
    anio smallint)
returns (
    nombremes varchar(25),
    monto_ventas numeric(12,2),
    monto_crecimiento numeric(12,2),
    crecimiento numeric(12,2))
as
declare variable mes smallint;
declare variable monto_totalnc numeric(12,2);
declare variable mes_anterior numeric(12,2);
begin
    -- Inicializo variables
    mes_anterior = 0;
    mes = 1;

    while (mes <= 12) do
    begin
        -- Calculo nombre de mes
        nombremes = decode(mes, 1, 'ENE', 2, 'FEB', 3, 'MAR', 4, 'ABR', 5, 'MAY', 6, 'JUN', 7, 'JUL', 8, 'AGO', 9, 'SEP', 10, 'OCT', 11, 'NOV', 12, 'DIC');

        -- Calculo FACTURAS.MONTOTOTAL
        select sum(f.montototal)
        from facturas_ventas f
        where
        extract(month from f.fecha) = :mes and
        extract(year from f.fecha) = :anio
        into :monto_ventas;

        -- Calculo NOTAS_CREADITOS.IMPORTE_TOTAL
        select sum(n.importe_total)
        from notas_creditos n
        where
        extract(month from n.fecha_actual) = :mes and
        extract(year from n.fecha_actual) = :anio
        into :monto_totalnc;

        -- Limpio datos
        monto_ventas = coalesce(monto_ventas, 0.00);
        monto_totalnc = coalesce(monto_totalnc, 0.00);

        -- Calculo MONTO_VENTAS
        monto_ventas = (monto_ventas - coalesce(monto_totalnc, 0.00));

        -- Calculo importe de crecimiento / decrecimiento
        monto_crecimiento = mes_anterior - monto_ventas;

        -- Calculo porcentaje de crecimiento / decrecimiento
        crecimiento = 0;
        if (monto_ventas <> 0) then
            crecimiento = (1 - (mes_anterior / monto_ventas)) * 100;

        -- Guardo MONTO_VENTAS para siguente vuelta del bucle con informacion del mes anterior
        mes_anterior = monto_ventas;

        -- Devuelvo datos para el mes
        suspend;

        mes = mes + 1;
    end
end;

novato_erick 26-01-2023 19:50:11

Resuelto: duda: aritmetica en procedimiento en Firebird
 
v:-)v
Cita:

Empezado por duilioisola (Mensaje 550174)
Yo lo haría así:

Código SQL [-]
        if (monto_ventas <> 0) then
            crecimiento = (1 - (mes_anterior / monto_ventas)) * 100;

        -- Guardo MONTO_VENTAS para siguente vuelta del bucle con informacion del mes anterior /*Esta era mi falla :eek: */ 
        mes_anterior = monto_ventas;

        -- Devuelvo datos para el mes
        suspend;

        mes = mes + 1;
    end
end;


Simplemente Claro tu respuesta y con los comentarios ^\||/

Gracias por tu excelente Respuesta duilioisola


La franja horaria es GMT +2. Ahora son las 01:28:50.

Powered by vBulletin® Version 3.6.8
Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.
Traducción al castellano por el equipo de moderadores del Club Delphi