Dynami c MySQL Кросс-таблица с датой диапазона и датой группы по имени и отображаемым значением в столбце даты по часам и минутам - PullRequest
0 голосов
/ 23 апреля 2020

У меня есть такая таблица:

+------+------------+---------------------+--------+
| name | department | tap_date            | status |
+------+------------+---------------------+--------+
| aa   | police     | 2020-04-02 22:37:00 | 10     |
+------+------------+---------------------+--------+
| aa   | police     | 2020-04-06 19:47:00 | 10     |
+------+------------+---------------------+--------+
| aa   | police     | 2020-04-07 03:57:00 | 20     |
+------+------------+---------------------+--------+
| aa   | police     | 2020-04-07 21:13:00 | 10     |
+------+------------+---------------------+--------+
| aa   | police     | 2020-04-08 06:21:00 | 20     |
+------+------------+---------------------+--------+
| aa   | police     | 2020-04-08 20:59:00 | 10     |
+------+------------+---------------------+--------+

И я хочу получить такой результат:

+------+------------+---------------+----------------+---------------+----------------+---------------+----------------+---------------+----------------+
| name | department | 2020-04-02_in | 2020-04-02_out | 2020-04-06_in | 2020-04-06_out | 2020-04-07_in | 2020-04-02_out | 2020-04-07_in | 2020-04-02_out |
+------+------------+---------------+----------------+---------------+----------------+---------------+----------------+---------------+----------------+
| aa   | police     |  22:37        | null           | 19:47         | null           | 03:57         | 21:13          | 06:21         | 20:59          |
+------+------------+---------------+----------------+---------------+----------------+---------------+----------------+---------------+----------------+

Вот мой текущий запрос:

SET SESSION group_concat_max_len = 1000000;

SET @sql = NULL;

SELECT
  GROUP_CONCAT(DISTINCT
     CONCAT(
      'CASE WHEN status=10 THEN GROUP_CONCAT(TIME_FORMAT(tap_date, \'%H:%i\')) END AS ',
      '`', DATE_FORMAT(tap_date, '%Y-%m-%d'), '`'
    )
  ) 
  INTO @sql
from absensi;

SET @sql = CONCAT('SELECT name, department_name, ', @sql, ' from absensi WHERE tap_date BETWEEN "2020-04-02" AND "2020-04-08" group by name');

 -- select @sql;

PREPARE stmt FROM @sql;
EXECUTE stmt;
DEALLOCATE PREPARE stmt;

И я получил что-то вроде этого:

+------+------------+-------------------------+-------------------------+-------------------------+-------------------------+-------------------------+-------------------------+-------------------------+------------+
| name | department | 2020-04-02              | 2020-04-03              | 2020-04-04              | 2020-04-05              | 2020-04-06              | 2020-04-07              | 2020-04-08              | 2020-04-09 |
+------+------------+-------------------------+-------------------------+-------------------------+-------------------------+-------------------------+-------------------------+-------------------------+------------+
| aa   | police     | 22:37,19:47,03:57,21:13 | 22:37,19:47,03:57,21:13 | 22:37,19:47,03:57,21:13 | 22:37,19:47,03:57,21:13 | 22:37,19:47,03:57,21:13 | 22:37,19:47,03:57,21:13 | 22:37,19:47,03:57,21:13 | 20:59      |
+------+------------+-------------------------+-------------------------+-------------------------+-------------------------+-------------------------+-------------------------+-------------------------+------------+
...