SELECT
 x.userid,
 x.name,
 x.email,
 DATE_FORMAT(x.tgl,'%Y-%m') AS periode,
 COUNT(DISTINCT CASE WHEN x.jenis='masuk' THEN x.tgl END) AS hari_masuk,
 COUNT(DISTINCT CASE WHEN x.jenis='pulang' THEN x.tgl END) AS hari_pulang,
 SUM(CASE WHEN x.ada_masuk=1 AND x.ada_pulang=1 THEN 1 ELSE 0 END) AS hari_lengkap
FROM (
 SELECT
 DATE(e.date_created) AS tgl,
 MAX(CASE WHEN m.meta_key='hidden-1' THEN m.meta_value END) AS userid,
 MAX(CASE WHEN m.meta_key='hidden-2' THEN m.meta_value END) AS name,
 MAX(CASE WHEN m.meta_key='hidden-3' THEN m.meta_value END) AS email,
 MAX(CASE WHEN m.meta_key='radio-1' THEN LOWER(m.meta_value) END) AS jenis,
 CASE WHEN MAX(CASE WHEN m.meta_key='radio-1' THEN LOWER(m.meta_value) END)='masuk' THEN 1 ELSE 0 END AS ada_masuk,
 CASE WHEN MAX(CASE WHEN m.meta_key='radio-1' THEN LOWER(m.meta_value) END)='pulang' THEN 1 ELSE 0 END AS ada_pulang
 FROM wpbw_frmt_form_entry e
 JOIN wpbw_frmt_form_entry_meta m ON m.entry_id=e.entry_id
 WHERE m.meta_key IN ('hidden-1','hidden-2','hidden-3','radio-1')
 GROUP BY e.entry_id, tgl
) x
GROUP BY x.userid, x.name, x.email, periode
ORDER BY periode DESC, x.name ASC