Skip to main content
6 de 8
se eliminaron 61 caracteres en el cuerpo

Creacion de Procedure(es el ejemplo de como crear la procedure aun no esta terminada):

DELIMITER $$

DROP PROCEDURE IF EXISTS `promedio` $$
    CREATE PROCEDURE `promedio`(in dateStart timestamp,in dateFinal timestamp,in idTenant int)
    BEGIN
    DECLARE cont int default 0;
    DECLARE dateProcesada timestamp;
    /*primer lunes*/
    SET dateProcesada= DATE_ADD(dateStart, INTERVAL - WEEKDAY(dateStart) DAY);
   
     WHILE dateProcesada  < dateFinal DO
    /*misma consulta la idea es crear 
     una tabla temporal para sw_sowing de forma tal q el max se calcule en este punto y devolver los datos en esa temporal*/
     SELECT pr_products.product,
    CONCAT(YEAR(dateProcesada),'-',LPAD(WEEK(dateProcesada),2,'0')) AS Week,
    SUM(IF(sw_sowing.type = 'SW', sw_sowing.quantity,0)) AS PlantSowing,
    SUM(IF(ROUND(DATEDIFF(TIMESTAMPADD(DAY,(6-  WEEKDAY(dateProcesada)),dateProcesada), sw_sowing.date)/7) >= pr_products.week_production AND sw_sowing.type = 'SW',sw_sowing.quantity,0)) AS              production
    FROM (
      SELECT max(sw_sowing.id) AS id
      FROM sw_sowing
      WHERE sw_sowing.status != 0
      AND sw_sowing.id_tenant=idTenant
      AND sw_sowing.status = 100
      AND sw_sowing.date <= TIMESTAMPADD(DAY,(6-WEEKDAY(dateProcesada)),dateProcesada)
     GROUP BY sw_sowing.id_production_unit_detail
      ) AS sw
     INNER JOIN sw_sowing ON sw_sowing.id = sw.id
     INNER JOIN pr_products ON pr_products.id = sw_sowing.id_product
     INNER JOIN pr_varieties ON sw_sowing.id_variety = pr_varieties.id
     INNER JOIN pr_lands ON pr_lands.id = sw_sowing.id_land
     WHERE pr_varieties.code != 1
     AND sw_sowing.id_product = 1
     AND sw_sowing.status = 100
     GROUP BY pr_products.product
     HAVING plantSowing > 0
     ORDER BY pr_products.product;
     /*sumar 7 dias para obtener proximo lunes*/
     SET dateProcesada= DATE_ADD(dateProcesada, INTERVAL 7 DAY);
    
    end while;
    END $$

DELIMITER ;

para llamarla usa esto:

$sql="call promedio('$firstDate',''$lastDate',:id_intent)";