MySQL - 动态数据透视表分组问题

时间:2016-07-06 07:43:05

标签: mysql sql stored-procedures group-by pivot-table

我正在尝试使用MySQL Prepared Statement创建一个动态数据透视表,我已经从众多其他有关MySQL数据透视表的问题中汇总了这些语句。但我没有得到我期待的输出。

我的表格如下:

技能包含员工可以接受培训的每项“技能”

| id | skill   | expiry_date |
 ----+---------+-------------
| 1  | SkillA  | 2016-07-01  |
| 2  | SkillB  | 2016-06-01  |
| 3  | SkillC  | 2016-04-01  |

employee_skills 包含员工接受技能培训的日期日志

| id | employee_id | skill_id | date_trained |
 ----+-------------+----------+--------------
| 1  | 1000001     | 1        | 2016-05-01   |
| 2  | 1000002     | 1        | 2016-05-02   |
| 3  | 1000001     | 2        | 2016-05-03   |
| 4  | 1000002     | 3        | 2016-05-04   |

campaign_skills 包含已分配给“广告系列”的技能。

| id | campaign_id | skill_id |
 ----+-------------+----------
| 1  | 1001        | 1        |
| 2  | 1001        | 3        |
| 3  | 1002        | 2        |

我还有一个包含员工详细信息(first_namelast_name等)的表格,其中employee_id为主键。

期望的结果

这是我需要在我的视图中显示的内容:

| Employee    | SkillA     | SkillB      | SkillC      |
 -------------+------------+-------------+-------------
| Person One  | 2016-05-01 | 2016-05-03  | -           | 
| Person Two  | 2016-05-02 | -           | 2016-05-04  |

显示的日期应为最新 employee_skills.date_trained

我还需要能够标记日期if ( employee_skills.date_trained < skills.expiry_date )。但是我可以稍后再说出来。

我的查询

我编写了一个以campaign_id作为参数的存储过程。我想我差不多了,但是我没有得到我期待的结果......

SET @sql = NULL;

SELECT 
    GROUP_CONCAT(DISTINCT
        CONCAT(
            'CASE WHEN skill = ''',
            s.skill,
            ''' THEN es.date_trained ELSE 0 END AS ',
            s.skill
        )
    ) INTO @sql
FROM  skills s
JOIN  campaign_skills cs ON cs.skill_id = s.id
WHERE cs.campaign_id = campaignID;

SET @sql = CONCAT('

    SELECT    CONCAT(e.first_name, " ", e.last_name) AS employee, ', @sql, ' 
    FROM      employees e
    LEFT JOIN employee_skills es ON es.employee_id = e.id
    LEFT JOIN campaign_skills cs ON cs.skill_id = es.skill_id
    LEFT JOIN skills s ON s.id = es.skill_id

    WHERE e.id IN (SELECT id FROM employees WHERE campaign_id = campaignID) 
        AND e.active = 1

    ORDER BY es.id DESC
    GROUP BY e.id, s.id

');

PREPARE statement FROM @sql;
EXECUTE statement;
DEALLOCATE PREPARE statement;

将输出如下内容:

| employee   | SkillA     | SkillB     |
 ------------+------------+------------
| Person One | 2015-05-04 | -          |
| Person One | -          | 2016-05-01 |
| Person Two | -          | 2016-03-25 |
| Person Two | 2016-04-04 | -          |

即使我正在使用GROUP。我已经坚持了一段时间,所以也许有人可以告诉我我的错误?

SQL FIDDLE:http://sqlfiddle.com/#!9/7caa6c/1

1 个答案:

答案 0 :(得分:1)

以下SQL可以作为解决问题的起点:

SELECT
  es.employee_id,
  CONCAT(e.first_name, " ", e.last_name) AS employee,
  MAX(IF (es.skill_id = 1, es.date_trained, null)) AS '1',
  MAX(IF (es.skill_id = 2, es.date_trained, null)) AS '2',
  MAX(IF (es.skill_id = 3, es.date_trained, null)) AS '3'
FROM
  employee_skills es
  LEFT JOIN employees e ON es.employee_id = e.id
GROUP BY
  es.employee_id 

结果是一个像这样的数据透视表:

| employee_id | employee   | 1          | 2          | 3          |
+-------------+------------+------------+------------+------------+
| 1001675     | Person Two | (null)     | 2016-07-02 | 2016-07-04 |
| 1006111     | Person One | 2016-07-01 | 2016-07-11 | (null)     |

如果动态创建SQL,则可以使用技能名称替换技能ID。此外,还可以在之后替换ID。