Mysql year/year grouping statement Java code SELECT count (DISTINCT (. rect_id) zcount,. job_dept, DATE_FORMAT (submit_date, '% Y-% m') zsubmit_date FROM table name a WHERE. statu = 3 AND. rstatu = 2 AND. job_dept IN ('19', '20', '21') group by. job_dept, DATE_FORMAT (submit_date, '% Y-% m') The key lies in DATE_FORMAT (submit_date,' % Y-% m ') grouping and sorting of the time month and month Java code SELECT zsubmit_date, MAX (case when job_dept = '19' THEN zcount ELSE 0 END) 19 zcount, MAX (case when job_dept = '20' THEN zcount ELSE 0 END) 20 zcount, MAX (case when job_dept = '21' THEN zcount ELSE 0 END) 21 zcount FROM (SELECT count (DISTINCT (. rect_id) zcount,. job_dept, DATE_FORMAT (submit_date, '% Y-% m') zsubmit_date FROM table name a WHERE. statu = 3 AND. rstatu = 2 AND. job_dept IN ('19', '20', '21') group by. job_dept, DATE_FORMAT (submit_date, '% Y-% m') q group by zsubmit_date The above is the column-to-row of mysql. The key point here is the usage of case when, which is used to perform column-to-row operations.