MySQL动态转置数据

时间:2018-07-05 08:55:50

标签: mysql

我想转置下表中的数据:

GUID |  ChecklistID   | DataCollectionID | data | Created_On 
0113   MT-B5-SectionA   MT-B5-0001         B101   2018-07-02 15:22:02 
0113   MT-B5-SectionA   MT-INSPECTION 1     OK    2018-07-02 15:22:02 
0113   MT-B5-SectionA   MT-B5-0002         B102   2018-07-02 15:22:02 
0113   MT-B5-SectionA   MT-INSPECTION 2     NG    2018-07-02 15:22:02 
0113   MT-B5-SectionA   MT-B5-0003         B103   2018-07-02 15:22:02 
0113   MT-B5-SectionA   MT-INSPECTION 3     OK    2018-07-02 15:22:02

像这样:

Created_On          | Roller Code | Inspection 
2018-07-02 15:22:02       B101        OK
2018-07-02 15:22:02       B102        NG
2018-07-02 15:22:02       B103        OK

但是使用此查询:

SELECT C.Created_On,
         MAX(CASE WHEN c.DataCollectionID like 'MT-B5%' THEN c.data ELSE NULL END) AS 'Roller Code',
         MAX(CASE WHEN c.DataCollectionID like 'MT-INSPECTION%' THEN c.data ELSE NULL END) AS 'Inspection',
    FROM checklist_detail c
    WHERE ChecklistID = 'MT-B5-SectionA' and Created_On like '2018-07-02%'

我只得到最后一行

Created_On          | Roller Code | Inspection 
2018-07-02 15:22:02       B103        OK

DataCollectionID或多或少是动态的。可以得到我想要的输出吗?

0 个答案:

没有答案