Advertisement
cdsatrian

test attendance

May 15th, 2017
164
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
MySQL 1.41 KB | None | 0 0
  1. CREATE TABLE tb_absensi(
  2.   id_absen INT AUTO_INCREMENT PRIMARY KEY,
  3.   id_karyawan INT,
  4.   jam_absen DATETIME
  5.  );
  6. INSERT INTO tb_absensi VALUES
  7. (1,1,'2017-05-12 05:45:00'),
  8. (2,1,'2017-05-12 14:08:00'),
  9. (3,2,'2017-05-12 13:46:00'),
  10. (4,2,'2017-05-12 22:16:00'),
  11. (5,3,'2017-05-12 22:08:00'),
  12. (6,3,'2017-05-13 06:08:00');
  13.  
  14.  
  15.  CREATE TABLE tb_shift(
  16.    id_shift INT AUTO_INCREMENT PRIMARY KEY,
  17.    nama_shift VARCHAR(10),
  18.    start_shift TIME,
  19.    end_shift TIME
  20.  );
  21.  
  22. INSERT INTO tb_shift VALUES
  23. (1,'SHIFT 1','06:00:00','13:59:00'),
  24. (2,'SHIFT 2','14:00:00','21:59:00'),
  25. (3,'SHIFT 3','22:00:00','05:59:00');
  26.  
  27.  CREATE TABLE tb_set_shift(
  28.    id_set INT AUTO_INCREMENT PRIMARY KEY,
  29.    id_kry INT,
  30.    id_shift_nya INT,
  31.    tgl_set_shift DATE
  32.  );
  33.  
  34. INSERT INTO tb_set_shift VALUES
  35. (1,1,1,'2017-05-12'),
  36. (2,2,2,'2017-05-12'),
  37. (3,3,3,'2017-05-12');
  38.  
  39. SELECT
  40. a.id_karyawan,
  41. IF(c.start_shift<c.end_shift,MIN(a.jam_absen),MAX(a.jam_absen)) AS mulai,
  42. IF(c.start_shift<c.end_shift,MAX(a.jam_absen),MIN(a.jam_absen)) AS selesai,
  43. IF(c.start_shift<c.end_shift,MAX(d.jam_absen),MIN(d.jam_absen)) AS selesai2,
  44. d.jam_absen
  45. FROM
  46. tb_absensi a
  47. LEFT JOIN tb_absensi d ON (a.id_karyawan=d.id_karyawan AND DATE(a.jam_absen)=SUBDATE(DATE(d.jam_absen), INTERVAL 1 DAY))
  48. JOIN tb_set_shift b ON (a.id_karyawan=b.id_kry AND DATE(a.jam_absen)=DATE(b.tgl_set_shift))
  49. JOIN tb_shift c ON (b.id_shift_nya=c.id_shift)
  50. GROUP BY a.id_karyawan
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement