Advertisement
Guest User

Untitled

a guest
Oct 15th, 2019
104
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.60 KB | None | 0 0
  1. select count(*) as jml
  2. from users;
  3.  
  4.  
  5. select username, bulan, jumlah
  6. from (select users_id, month(logs.created_at) as bulan, count(*) as jumlah
  7. from logs
  8. where year(logs.created_at) = 2019
  9. and users_id in (select users_id
  10. from (
  11. select users_id, count(*) as jml
  12. from logs
  13. group by users_id
  14. order by jml desc
  15. limit 2) sub_dua_terbanyak)
  16. group by users_id, month(created_at)
  17. order by users_id, bulan) a
  18. join users on a.users_id = users.id;
  19.  
  20.  
  21. #gampang
  22. (select users_id, month(created_at) as bulan, count(*) as jumlah
  23. from logs
  24. where year(created_at) = 2019
  25. and users_id = 89
  26. group by month(created_at)
  27. order by bulan)
  28. UNION
  29. (select users_id, month(created_at) as bulan, count(*) as jumlah
  30. from logs
  31. where year(created_at) = 2019
  32. and users_id = 80
  33. group by month(created_at)
  34. order by bulan);
  35.  
  36. #lebih efisien where in
  37. select users_id, month(created_at) as bulan, count(*) as jumlah
  38. from logs
  39. where year(created_at) = 2019
  40. and users_id in (89, 80)
  41. group by users_id, month(created_at)
  42. order by users_id, bulan
  43.  
  44. #ganti user_id mjd username
  45. select username, bulan, jumlah
  46. from (
  47. select users_id, month(created_at) as bulan, count(*) as jumlah
  48. from logs
  49. where year(created_at) = 2019
  50. and users_id in (89, 80)
  51. group by users_id, month(created_at)
  52. order by users_id, bulan
  53. ) sub_query_1
  54. join users on sub_query_1.users_id = users.id;
  55.  
  56. #menampilkan 2 orang yang aktifitasnya paling tinggi
  57. select users_id
  58. from (
  59. select users_id, count(*) as jumlah
  60. from logs
  61. group by users_id
  62. order by jumlah desc
  63. limit 2
  64. ) sub_query_2;
  65.  
  66.  
  67. #tampilkan log statistik 2 orang yang paling tinggi
  68. select username, bulan, jumlah
  69. from (
  70. select users_id, month(created_at) as bulan, count(*) as jumlah
  71. from logs
  72. where year(created_at) = 2019
  73. and users_id in (select users_id
  74. from (
  75. select users_id, count(*) as jumlah
  76. from logs
  77. group by users_id
  78. order by jumlah desc
  79. limit 2
  80. ) sub_query_2)
  81. group by users_id, month(created_at)
  82. order by users_id, bulan
  83. ) sub_query_1
  84. join users on sub_query_1.users_id = users.id;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement