Advertisement
peponloqui

SQL

Jul 10th, 2019
200
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
SQL 1.74 KB | None | 0 0
  1. CREATE OR ALTER PROCEDURE VKY_ZZZZ_TEMP
  2. RETURNS (
  3.     ANHO SMALLINT,
  4.     CLIENTE VARCHAR(1024),
  5.     CANTIDAD INTEGER,
  6.     TEXTO_ARMADO VARCHAR(32))
  7. AS
  8. DECLARE variable ID_CLIENTE_L INTEGER;
  9. DECLARE variable CANTIDAD_ANHOS_ANTERIORES INTEGER;
  10. BEGIN
  11.  FOR--1er. for select, de distintos años, en caso de no tener ninguna tabla años
  12.   SELECT EXTRACT(YEAR FROM f.fecha_documento) AS anho
  13.   FROM facturaciones f
  14.   GROUP BY EXTRACT(YEAR FROM f.fecha_documento)
  15.   INTO :anho
  16.  do BEGIN
  17.    --2do. for select, Clientes
  18.    FOR
  19.      SELECT c.cliente, c.id_cliente
  20.      FROM clientes_v c
  21.      WHERE c.cliente containing 'S.A.'
  22.      ORDER  BY c.cliente
  23.      INTO :cliente, :id_cliente_l
  24.    do BEGIN
  25.       --obtengo la cantidad de facturas emitidas al cliente, en el año dado.
  26.       SELECT COUNT(f.id_factura)
  27.       FROM facturaciones f
  28.       WHERE f.id_cliente = :id_cliente_l
  29.         AND EXTRACT(YEAR FROM f.fecha_documento) = :anho
  30.         AND UPPER(f.estado) <> UPPER('anulado')
  31.       INTO :cantidad;
  32.  
  33.       --obtengo la cantidad de facturas emitidas al cliente, en años anteriores al año en cuestion
  34.       SELECT COUNT(f.id_factura)
  35.       FROM facturaciones f
  36.       WHERE f.id_cliente = :id_cliente_l
  37.         AND EXTRACT(YEAR FROM f.fecha_documento) < :anho
  38.         AND UPPER(f.estado) <> UPPER('anulado')
  39.       INTO :cantidad_anhos_anteriores;
  40.  
  41.       IF (:cantidad > 0 AND :cantidad_anhos_anteriores > 1) THEN
  42.         texto_armado = 'Re-Compra';
  43.       ELSE IF (:cantidad > 0 AND :cantidad_anhos_anteriores = 0) THEN
  44.         texto_armado = 'Compra';
  45.       ELSE IF (:cantidad = 0) THEN
  46.         texto_armado = 'NO-Compra';
  47.  
  48.      suspend;
  49.    END--fin 2do. for select, clientes
  50.  END---fin 1er. for select, para extraer los distintos años
  51. END;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement