Advertisement
cdsatrian

shiftlog-machines

Aug 8th, 2012
56
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
MySQL 2.36 KB | None | 0 0
  1. DELIMITER $$
  2. DROP PROCEDURE IF EXISTS `mysp`$$
  3. CREATE PROCEDURE `mysp`(
  4. IN i_date DATE,
  5. IN i_group INT,
  6. IN i_oprtr INT
  7. )
  8. BEGIN
  9.  -- DECLARE variables
  10.  DECLARE prepsql varchar(1000);
  11.  DECLARE no_more_rows BOOLEAN DEFAULT FALSE;
  12.  DECLARE dstamp_val VARCHAR(20);
  13.  DECLARE dstamp_cur CURSOR FOR
  14.          SELECT DISTINCT dstamp
  15.          FROM shiftlog
  16.          WHERE dstamp BETWEEN i_date AND DATE_ADD(i_date,INTERVAL 6 DAY)
  17.          ORDER BY dstamp;
  18.  DECLARE continue HANDLER FOR NOT FOUND
  19.          SET no_more_rows = TRUE;
  20.  -- start generating sql
  21.  SET prepsql = 'SELECT s.mcode, m.speed_ref, m.d_group, m.optr_grp ';
  22.  OPEN dstamp_cur;
  23.  the_loop: LOOP
  24.          -- fetch data
  25.          FETCH dstamp_cur
  26.          INTO dstamp_val;
  27.          -- break out loop
  28.          IF no_more_rows THEN
  29.             CLOSE dstamp_cur;
  30.             LEAVE the_loop;
  31.          END IF;
  32.          -- display
  33.          SET prepsql = CONCAT(prepsql,'\n, CASE WHEN dstamp=\'',dstamp_val,'\' THEN IFNULL((h23-h16),0) else 0 end  "',dstamp_val,'"');
  34.  END LOOP the_loop;
  35.  SET prepsql = CONCAT(prepsql,'\nFROM shiftlog s LEFT JOIN machines m ON s.mcode=m.mcode WHERE m.mloc=i_group AND s.dstamp=i_date AND m.oprtr_grp=i_oprtr');
  36.  -- executing generated query
  37.  SET @mysql = prepsql;
  38.  PREPARE stmt FROM @mysql;
  39.  EXECUTE stmt;
  40.  -- clean up
  41.  DROP PREPARE stmt;
  42. END$$
  43. DELIMITER;
  44. -- call mysp($from,$group,$nim);
  45.  
  46.  
  47. -- atau gunakan ini, untuk jam1 s.d. jam7 dan speed1 s.d speed7, hitung via php programming
  48. SELECT s.mcode AS mcode2,
  49.    m.speed_ref,
  50.    m.d_group,
  51.    m.oprtr_grp,
  52.   (h23 - h16) as cntr1,
  53.   (SELECT (h23 - h16) FROM shiftlog WHERE dstamp=DATE_ADD('$from',INTERVAL 1 DAY) AND mcode=mcode2) AS cntr2,
  54.   (SELECT (h23 - h16) FROM shiftlog WHERE dstamp=DATE_ADD('$from',INTERVAL 2 DAY) AND mcode=mcode2) AS cntr3,
  55.   (SELECT (h23 - h16) FROM shiftlog WHERE dstamp=DATE_ADD('$from',INTERVAL 3 DAY) AND mcode=mcode2) AS cntr4,
  56.   (SELECT (h23 - h16) FROM shiftlog WHERE dstamp=DATE_ADD('$from',INTERVAL 4 DAY) AND mcode=mcode2) AS cntr5,
  57.   (SELECT (h23 - h16) FROM shiftlog WHERE dstamp=DATE_ADD('$from',INTERVAL 5 DAY) AND mcode=mcode2) AS cntr6,
  58.   (SELECT (h23 - h16) FROM shiftlog WHERE dstamp=DATE_ADD('$from',INTERVAL 6 DAY) AND mcode=mcode2) AS cntr7,
  59. FROM  shiftlog s
  60. LEFT JOIN machines m
  61.   ON s.mcode = m.mcode
  62. WHERE m.mloc = '$group'
  63.   AND s.dstamp = '$from'
  64.   AND m.oprtr_grp = '$nim'"
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement