1

100% 機能する以下のクエリがあります。

SELECT
transporttype,
concat(MONTHNAME(STR_TO_DATE(month, '%m')), ' ', year) AS `month`,
round(sum(cost),0) AS cost
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

これにより、以下のように結果が 1 列に出力されます。

ここに画像の説明を入力

出力が列になるようにクエリを操作して、グラフ化できるようにするにはどうすればよいですか。望ましい出力は次のようなものです:

ここに画像の説明を入力

いつもありがとうございます。

Oscar Pérez からの可能な回答に合わせて更新

ここに画像の説明を入力

4

2 に答える 2

1

LEFT JOIN演算子を使用できます。例えば:

SELECT s0.month, 
       s1.cost as Inbound, 
       s2.cost as LocalPMB,
       s3.cost as Shuttle,    
       s4.cost as LongDistance
  FROM (
         SELECT 1 as month
          UNION
         SELECT 2 as month
          UNION
         SELECT 3 as month
          UNION
         SELECT 4 as month
          UNION
         SELECT 5 as month
          UNION
         SELECT 6 as month
          UNION
         SELECT 7 as month
          UNION
         SELECT 8 as month
          UNION
         SELECT 9 as month
          UNION
         SELECT 10 as month
          UNION
         SELECT 11 as month
          UNION
         SELECT 12 as month
       ) as s0
LEFT JOIN
       (
         SELECT month,
                round(sum(cost),0) AS cost
           FROM v2ReportingTable
          WHERE urgent='no'
            AND transporttype='Inbound'
       GROUP BY month
       ) as s1 on s0.month=s1.month
LEFT JOIN
       (
         SELECT month,
                round(sum(cost),0) AS cost
           FROM v2ReportingTable
          WHERE urgent='no'
            AND transporttype='LocalPMB'
       GROUP BY month
       ) as s2 on s0.month=s2.month
LEFT JOIN
       (
         SELECT month,
                round(sum(cost),0) AS cost
           FROM v2ReportingTable
          WHERE urgent='no'
            AND transporttype='Shuttle'
       GROUP BY month
       ) as s3 on s0.month=s3.month
LEFT JOIN
       (
         SELECT month,
                round(sum(cost),0) AS cost
           FROM v2ReportingTable
          WHERE urgent='no'
            AND transporttype='Long Distance'
       GROUP BY month
       ) as s4 on s0.month=s4.month
ORDER BY month
于 2013-01-25T10:50:01.570 に答える
0

@JWのように、ご協力いただきありがとうございます。

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-28T14:11:49.833 に答える