2013-01-25 96 views
1

我有以下查詢,工作100%。mysql查詢輸出行到列

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列的結果:

enter image description here

如何操作查詢,以便輸出爲列,所以我可以繪製它。所需的輸出是這樣的:

enter image description here

幫助表示讚賞一如既往,

更新到可能的答案,從奧斯卡·佩雷斯

enter image description here

+0

這裏沒有什麼可看的......'1列如下:'和'output is like'? – Sahal

+0

這是您查詢的PIVOT表。 [這是一個很好的解釋。](http://www.artfulsoftware.com/infotree/queries.php#78) – valex

回答

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` 
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 
+0

謝謝奧斯卡,效果不錯,但不完全。如果第2,3,4個月沒有數據,由於連接的列沒有數據,第1個月的相應數據不存在?我們如何強制它顯示第一個月的所有數據,以及第二個月,第三個月的數據是否可用等等。外連接?謝謝,瑞安 – Smudger

+0

編輯迴應。現在你會得到12行和相應的列。每個列在沒有數據的月份都將爲空。 –

+0

嗨,這隻顯示Inbound的值。因爲沒有其他'傳輸類型'的值,它顯示爲空白,因爲每個更新後的圖像... – Smudger