Advertisement
cdsatrian

grouping by kode on max datetime

Jan 23rd, 2017
219
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
MySQL 0.84 KB | None | 0 0
  1. CREATE TABLE tabel(
  2. kode_brg INT,
  3. tgl_stock DATETIME
  4. );
  5.  
  6. INSERT INTO tabel VALUES
  7. (1,'2017-01-22 23:55:57'),
  8. (1,'2017-01-22 23:44:07'),
  9. (2,'2017-01-22 21:10:24'),
  10. (1,'2017-01-22 19:52:57');
  11.  
  12. SELECT * FROM tabel;
  13.  
  14. +----------+---------------------+
  15. | kode_brg | tgl_stock           |
  16. +----------+---------------------+
  17. |        1 | 2017-01-22 23:55:57 |
  18. |        1 | 2017-01-22 23:44:07 |
  19. |        2 | 2017-01-22 21:10:24 |
  20. |        1 | 2017-01-22 19:52:57 |
  21. +----------+---------------------+
  22. 4 rows in set (0.00 sec)
  23.  
  24. SELECT kode_brg,MAX(tgl_stock) AS tgl_stock
  25. FROM tabel
  26. GROUP BY kode_brg;
  27.  
  28. +----------+---------------------+
  29. | kode_brg | tgl_stock           |
  30. +----------+---------------------+
  31. |        1 | 2017-01-22 23:55:57 |
  32. |        2 | 2017-01-22 21:10:24 |
  33. +----------+---------------------+
  34. 2 rows in set (0.00 sec)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement