2

我有 MySQL 查询:

SELECT
transporttype,
concat(MONTHNAME(STR_TO_DATE(month, '%m')), ' ', year) AS `month`,
count(loadnumber) AS loads
FROM v2ReportingTable
WHERE (transporttype not in ('Extrusions-LongDistance','Extrusions-Shuttle') and urgent='no')
GROUP BY (concat(MONTHNAME(STR_TO_DATE(month, '%m')),' ',year)),transporttype
ORDER BY (concat(MONTHNAME(STR_TO_DATE(month, '%m')),' ',year)), transporttype

产生以下输出:

在此处输入图像描述

我想以列格式显示结果,如下所示: 在此处输入图像描述

如何修改我的查询以显示结果。

一如既往地感谢您的帮助

4

1 回答 1

2
SELECT  `month`,
        MAX(CASE WHEN transporttype = 'Inbound' THEN loads ELSE NULL END) `Inbound`,
        MAX(CASE WHEN transporttype = 'LocalGauteng' THEN loads ELSE NULL END) `LocalGauteng`,
        MAX(CASE WHEN transporttype = 'LongDistance' THEN loads ELSE NULL END) `LongDistance`,
        MAX(CASE WHEN transporttype = 'Shuttle-company1' THEN loads ELSE NULL END) `Shuttle-company1`,
        MAX(CASE WHEN transporttype = 'Shuttle-company2' THEN loads ELSE NULL END) `Shuttle-company2`,
        MAX(CASE WHEN transporttype = 'stores' THEN loads ELSE NULL END) `stores`,
        MAX(CASE WHEN transporttype = 'returns' THEN loads ELSE NULL END) `returns`,
        MAX(CASE WHEN transporttype = 'localkzn' THEN loads ELSE NULL END) `localkzn`
FROM
    (
        SELECT  transporttype,
                CONCAT(MONTHNAME(STR_TO_DATE(month, '%m')), ' ', year) AS `month`,
                COUNT(loadnumber) AS loads
        FROM    v2ReportingTable
        WHERE   transporttype not in ('Extrusions-LongDistance','Extrusions-Shuttle') AND
                urgent='no'
        GROUP BY (concat(MONTHNAME(STR_TO_DATE(month, '%m')),' ',year)), transporttype
    ) aa
GROUP BY `month`
于 2013-01-28T13:52:33.683 回答