如何在mysql中将输出行转换为列。我试图根据两个不同的原因提取同一件事。以下是两个条件。
1)ReportType ='FinalReport'
2)ReportType ='InterimReport'
下面是我的查询
SELECT ReportID,PackageID,
MAX(CASE WHEN CheckID = '1' THEN CheckID ELSE NULL END) AS check1,
MAX(CASE WHEN CheckID = '2' THEN CheckID ELSE NULL END) AS check2,
MAX(CASE WHEN CheckID = '3' THEN CheckID ELSE NULL END) AS check3,
MAX(CASE WHEN CheckID = '4' THEN CheckID ELSE NULL END) AS check4,
MAX(CASE WHEN CheckID = '5' THEN CheckID ELSE NULL END) AS check5,
MAX(CASE WHEN CheckID = '6' THEN CheckID ELSE NULL END) AS check6,
MAX(CASE WHEN CheckID = '7' THEN CheckID ELSE NULL END) AS check7,
MAX(CASE WHEN CheckID = '8' THEN CheckID ELSE NULL END) AS check8,
MAX(CASE WHEN CheckID = '9' THEN CheckID ELSE NULL END) AS check9,
MAX(CASE WHEN CheckID = '10' THEN CheckID ELSE NULL END) AS check10,
ReportType as reporttype
FROM sla_package WHERE PackageID = '134' and ReportType='FinalReport' UNION
SELECT ReportID,PackageID,
MAX(CASE WHEN CheckID = '1' THEN CheckID ELSE NULL END) AS check12,
MAX(CASE WHEN CheckID = '2' THEN CheckID ELSE NULL END) AS check22,
MAX(CASE WHEN CheckID = '3' THEN CheckID ELSE NULL END) AS check32,
MAX(CASE WHEN CheckID = '4' THEN CheckID ELSE NULL END) AS check42,
MAX(CASE WHEN CheckID = '5' THEN CheckID ELSE NULL END) AS check52,
MAX(CASE WHEN CheckID = '6' THEN CheckID ELSE NULL END) AS check62,
MAX(CASE WHEN CheckID = '7' THEN CheckID ELSE NULL END) AS check72,
MAX(CASE WHEN CheckID = '8' THEN CheckID ELSE NULL END) AS check82,
MAX(CASE WHEN CheckID = '9' THEN CheckID ELSE NULL END) AS check92,
MAX(CASE WHEN CheckID = '10' THEN CheckID ELSE NULL END) AS check102,
ReportType as reporttype2
FROM sla_package WHERE PackageID = '134' and ReportType='InterimReport'
其输出如下所示
但是我想要输出为
ReportID1 PackageID check1 check2 check3 check4 check5 check6 check7 check8 check9 check10 reporttype1 RepoerID2 check11 check12 check13 check14 check15 check16 check17 check18 check19 check110 reporttype2
2192 134 NULL NULL 3 NULL 5 NULL NULL NULL NULL NULL FinalReport 5693 NULL NULL 3 NULL 5 NULL NULL NULL NULL NULL InterimRepo