преобразовать строку вывода в столбец в MySQL на основе идентификатора - PullRequest
0 голосов
/ 26 февраля 2019

Как преобразовать выходную строку в столбец в 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'

и это вывод, как показано ниже

enter image description here

Но я хочу вывод как

 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
...