Advertisement
cdsatrian

presensi

Oct 15th, 2014
298
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
MySQL 4.64 KB | None | 0 0
  1. CREATE TABLE IF NOT EXISTS tb_karyawan(
  2.   nik VARCHAR(10) NOT NULL PRIMARY KEY,
  3.   nama VARCHAR(30) NOT NULL
  4. ) ENGINE=MyISAM;
  5.  
  6. INSERT INTO tb_karyawan
  7. VALUES
  8.   ('123456789','Dika Nanda'),
  9.   ('1234567890','Andika Dwi Chahya'),
  10.   ('2147483647','Akbar Alifian');
  11.  
  12. SELECT * FROM tb_karyawan;
  13.  
  14. +------------+-------------------+
  15. | nik        | nama              |
  16. +------------+-------------------+
  17. | 123456789  | Dika Nanda        |
  18. | 1234567890 | Andika Dwi Chahya |
  19. | 2147483647 | Akbar Alifian     |
  20. +------------+-------------------+
  21. 3 rows in set (0.00 sec)
  22.  
  23. CREATE TABLE IF NOT EXISTS tb_presensi(
  24.   nik VARCHAR(10) NOT NULL,
  25.   tanggal DATE,
  26.   jam_masuk TIME,
  27.   jam_pulang TIME,
  28.   terlambat TIME,
  29.   ket VARCHAR(30) NOT NULL
  30. ) ENGINE=MyISAM;
  31.  
  32. INSERT INTO tb_presensi
  33. VALUES
  34.   ('123456789','2014-10-15','11:30:17','00:00:00','03:30:00','masuk'),
  35.   ('1234567890','2014-10-11','12:25:01','12:26:27','04:25:00','masuk'),
  36.   ('1234567890','2014-10-12','15:02:42','00:00:00','07:02:00','masuk'),
  37.   ('1234567890','2014-10-14','09:48:47','10:42:53','01:48:00','masuk');
  38.  
  39. SELECT * FROM tb_presensi;
  40.  
  41. +------------+------------+-----------+------------+-----------+-------+
  42. | nik        | tanggal    | jam_masuk | jam_pulang | terlambat | ket   |
  43. +------------+------------+-----------+------------+-----------+-------+
  44. | 123456789  | 2014-10-15 | 11:30:17  | 00:00:00   | 03:30:00  | masuk |
  45. | 1234567890 | 2014-10-11 | 12:25:01  | 12:26:27   | 04:25:00  | masuk |
  46. | 1234567890 | 2014-10-12 | 15:02:42  | 00:00:00   | 07:02:00  | masuk |
  47. | 1234567890 | 2014-10-14 | 09:48:47  | 10:42:53   | 01:48:00  | masuk |
  48. +------------+------------+-----------+------------+-----------+-------+
  49. 4 rows in set (0.00 sec)
  50.  
  51. SELECT
  52.   x1.tanggal,
  53.   x1.nik,
  54.   x1.nama,
  55.   IF(x2.jam_masuk,x2.jam_masuk,'-') AS masuk,
  56.   IF(x2.jam_pulang,x2.jam_pulang,'-') AS pulang,
  57.   IF(x2.terlambat,x2.terlambat,'-') AS terlambat,
  58.   IF(x2.ket IS NULL ,'tidak masuk',x2.ket) AS ket
  59. FROM
  60.   (
  61.     SELECT  
  62.       z1.tanggal,z1.t1,z1.t2,
  63.       z2.nik,z2.nama
  64.     FROM
  65.     (
  66.       SELECT
  67.         DATE_ADD(t.t1,INTERVAL td.digit DAY) AS tanggal,t.t1,t.t2
  68.       FROM
  69.         (
  70.           SELECT
  71.             (b1.d+b2.d+b4.d+b8.d+b16.d) AS digit
  72.           FROM
  73.             (SELECT 0 d UNION ALL SELECT 1 d ) b1
  74.             CROSS JOIN (SELECT 0 d UNION ALL SELECT 2 d ) b2
  75.             CROSS JOIN (SELECT 0 d UNION ALL SELECT 4 d ) b4
  76.             CROSS JOIN (SELECT 0 d UNION ALL SELECT 8 d ) b8
  77.             CROSS JOIN (SELECT 0 d UNION ALL SELECT 16 d ) b16
  78.         ) td,
  79.         (
  80.           SELECT
  81.             MIN(tanggal) AS t1, MAX(tanggal) AS t2
  82.           FROM
  83.             tb_presensi
  84.         ) t
  85.       ) z1
  86.     ,
  87.     (
  88.       SELECT
  89.         a.nik,a.nama
  90.       FROM
  91.         tb_karyawan a
  92.     ) z2
  93.   ) x1
  94.   LEFT JOIN
  95.     tb_presensi x2 USING(nik,tanggal)
  96. WHERE
  97.   x1.tanggal BETWEEN x1.t1 AND x1.t2
  98. ORDER BY
  99.   x1.tanggal,x1.nik;
  100.  
  101. +------------+------------+-------------------+----------+----------+-----------+-------------+
  102. | tanggal    | nik        | nama              | masuk    | pulang   | terlambat| ket         |
  103. +------------+------------+-------------------+----------+----------+-----------+-------------+
  104. | 2014-10-11 | 123456789  | Dika Nanda        | -        | -        | -| tidak masuk |
  105. | 2014-10-11 | 1234567890 | Andika Dwi Chahya | 12:25:01 | 12:26:27 | 04:25:00| masuk       |
  106. | 2014-10-11 | 2147483647 | Akbar Alifian     | -        | -        | -| tidak masuk |
  107. | 2014-10-12 | 123456789  | Dika Nanda        | -        | -        | -| tidak masuk |
  108. | 2014-10-12 | 1234567890 | Andika Dwi Chahya | 15:02:42 | -        | 07:02:00| masuk       |
  109. | 2014-10-12 | 2147483647 | Akbar Alifian     | -        | -        | -| tidak masuk |
  110. | 2014-10-13 | 123456789  | Dika Nanda        | -        | -        | -| tidak masuk |
  111. | 2014-10-13 | 1234567890 | Andika Dwi Chahya | -        | -        | -| tidak masuk |
  112. | 2014-10-13 | 2147483647 | Akbar Alifian     | -        | -        | -| tidak masuk |
  113. | 2014-10-14 | 123456789  | Dika Nanda        | -        | -        | -| tidak masuk |
  114. | 2014-10-14 | 1234567890 | Andika Dwi Chahya | 09:48:47 | 10:42:53 | 01:48:00| masuk       |
  115. | 2014-10-14 | 2147483647 | Akbar Alifian     | -        | -        | -| tidak masuk |
  116. | 2014-10-15 | 123456789  | Dika Nanda        | 11:30:17 | -        | 03:30:00| masuk       |
  117. | 2014-10-15 | 1234567890 | Andika Dwi Chahya | -        | -        | -| tidak masuk |
  118. | 2014-10-15 | 2147483647 | Akbar Alifian     | -        | -        | -| tidak masuk |
  119. +------------+------------+-------------------+----------+----------+-----------+-------------+
  120. 15 rows in set (0.00 sec)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement