2017-03-08 196 views
2

我有一個表的工作具有以下結構GROUP BY與CASE語句

================================================ 
| userID | jobDate | cityID | totalCost | 
================================================ 
| 1234 | 2016-04-01 | 1  |  200 | 
| 1234 | 2016-04-21 | 1  |  800 | 
| 1234 | 2016-08-03 | 1  |  2000 | 
| 1234 | 2016-12-01 | 1  |  300 | 
| 1234 | 2017-02-01 | 1  |  500 | 
================================================ 

我想一個月調換成列。

預期輸出:

======================================================== 
| userID | April 2016 | May 2016 | .... | January 2017 | 
======================================================== 
| 1234 | 1000 | 0  | .... |  500  | 
======================================================== 

我的查詢:

SELECT u.userID, u.userFullName, ci.cityName, 
    CASE 
     WHEN CONCAT(MONTHNAME(j.jobDate), ' ', YEAR(j.jobDate)) = 'April 2016' THEN GROUP_CONCAT(CONCAT(c.categoryName, ' ', s.supercategoryName)) 
     ELSE '-' 
    END AS 'April 2016 Services', 
    CASE 
     WHEN CONCAT(MONTHNAME(j.jobDate), ' ', YEAR(j.jobDate)) = 'April 2016' THEN SUM(j.totalCost + j.discountReceived) 
     ELSE 0 
    END AS 'April 2016 Spends', 

    CASE 
     WHEN CONCAT(MONTHNAME(j.jobDate), ' ', YEAR(j.jobDate)) = 'May 2016' THEN GROUP_CONCAT(CONCAT(c.categoryName, ' ', s.supercategoryName)) 
     ELSE '-' 
    END AS 'May 2016 Services', 
    CASE 
     WHEN CONCAT(MONTHNAME(j.jobDate), ' ', YEAR(j.jobDate)) = 'May 2016' THEN SUM(j.totalCost + j.discountReceived) 
     ELSE 0 
    END AS 'May 2016 Spends', 

    CASE 
     WHEN CONCAT(MONTHNAME(j.jobDate), ' ', YEAR(j.jobDate)) = 'June 2016' THEN GROUP_CONCAT(CONCAT(c.categoryName, ' ', s.supercategoryName)) 
     ELSE '-' 
    END AS 'June 2016 Services', 
    CASE 
     WHEN CONCAT(MONTHNAME(j.jobDate), ' ', YEAR(j.jobDate)) = 'June 2016' THEN SUM(j.totalCost + j.discountReceived) 
     ELSE 0 
    END AS 'June 2016 Spends', 

    CASE 
     WHEN CONCAT(MONTHNAME(j.jobDate), ' ', YEAR(j.jobDate)) = 'July 2016' THEN GROUP_CONCAT(CONCAT(c.categoryName, ' ', s.supercategoryName)) 
     ELSE '-' 
    END AS 'July 2016 Services', 
    CASE 
     WHEN CONCAT(MONTHNAME(j.jobDate), ' ', YEAR(j.jobDate)) = 'July 2016' THEN SUM(j.totalCost + j.discountReceived) 
     ELSE 0 
    END AS 'July 2016 Spends', 

    CASE 
     WHEN CONCAT(MONTHNAME(j.jobDate), ' ', YEAR(j.jobDate)) = 'August 2016' THEN GROUP_CONCAT(CONCAT(c.categoryName, ' ', s.supercategoryName)) 
     ELSE '-' 
    END AS 'August 2016 Services', 
    CASE 
     WHEN CONCAT(MONTHNAME(j.jobDate), ' ', YEAR(j.jobDate)) = 'August 2016' THEN SUM(j.totalCost + j.discountReceived) 
     ELSE 0 
    END AS 'August 2016 Spends', 

    CASE 
     WHEN CONCAT(MONTHNAME(j.jobDate), ' ', YEAR(j.jobDate)) = 'September 2016' THEN GROUP_CONCAT(CONCAT(c.categoryName, ' ', s.supercategoryName)) 
     ELSE '-' 
    END AS 'September 2016 Services', 
    CASE 
     WHEN CONCAT(MONTHNAME(j.jobDate), ' ', YEAR(j.jobDate)) = 'September 2016' THEN SUM(j.totalCost + j.discountReceived) 
     ELSE 0 
    END AS 'September 2016 Spends', 

    CASE 
     WHEN CONCAT(MONTHNAME(j.jobDate), ' ', YEAR(j.jobDate)) = 'October 2016' THEN GROUP_CONCAT(CONCAT(c.categoryName, ' ', s.supercategoryName)) 
     ELSE '-' 
    END AS 'October 2016 Services', 
    CASE 
     WHEN CONCAT(MONTHNAME(j.jobDate), ' ', YEAR(j.jobDate)) = 'October 2016' THEN SUM(j.totalCost + j.discountReceived) 
     ELSE 0 
    END AS 'October 2016 Spends', 

    CASE 
     WHEN CONCAT(MONTHNAME(j.jobDate), ' ', YEAR(j.jobDate)) = 'November 2016' THEN GROUP_CONCAT(CONCAT(c.categoryName, ' ', s.supercategoryName)) 
     ELSE '-' 
    END AS 'November 2016 Services', 
    CASE 
     WHEN CONCAT(MONTHNAME(j.jobDate), ' ', YEAR(j.jobDate)) = 'November 2016' THEN SUM(j.totalCost + j.discountReceived) 
     ELSE 0 
    END AS 'November 2016 Spends', 

    CASE 
     WHEN CONCAT(MONTHNAME(j.jobDate), ' ', YEAR(j.jobDate)) = 'December 2016' THEN GROUP_CONCAT(CONCAT(c.categoryName, ' ', s.supercategoryName)) 
     ELSE '-' 
    END AS 'December 2016 Services', 
    CASE 
     WHEN CONCAT(MONTHNAME(j.jobDate), ' ', YEAR(j.jobDate)) = 'December 2016' THEN SUM(j.totalCost + j.discountReceived) 
     ELSE 0 
    END AS 'December 2016 Spends', 

    CASE 
     WHEN CONCAT(MONTHNAME(j.jobDate), ' ', YEAR(j.jobDate)) = 'January 2017' THEN GROUP_CONCAT(CONCAT(c.categoryName, ' ', s.supercategoryName)) 
     ELSE '-' 
    END AS 'January 2017 Services', 
    CASE 
     WHEN CONCAT(MONTHNAME(j.jobDate), ' ', YEAR(j.jobDate)) = 'January 2017' THEN SUM(j.totalCost + j.discountReceived) 
     ELSE 0 
    END AS 'January 2017 Spends', 

    CASE 
     WHEN CONCAT(MONTHNAME(j.jobDate), ' ', YEAR(j.jobDate)) = 'February 2017' THEN GROUP_CONCAT(CONCAT(c.categoryName, ' ', s.supercategoryName)) 
     ELSE '-' 
    END AS 'February 2017 Services', 
    CASE 
     WHEN CONCAT(MONTHNAME(j.jobDate), ' ', YEAR(j.jobDate)) = 'February 2017' THEN SUM(j.totalCost + j.discountReceived) 
     ELSE 0 
    END AS 'February 2017 Spends', 

    CASE 
     WHEN CONCAT(MONTHNAME(j.jobDate), ' ', YEAR(j.jobDate)) = 'March 2017' THEN GROUP_CONCAT(CONCAT(c.categoryName, ' ', s.supercategoryName)) 
     ELSE '-' 
    END AS 'March 2017 Services', 
    CASE 
     WHEN CONCAT(MONTHNAME(j.jobDate), ' ', YEAR(j.jobDate)) = 'March 2017' THEN SUM(j.totalCost + j.discountReceived) 
     ELSE 0 
    END AS 'March 2017 Spends' 
FROM jobs j 
LEFT JOIN categories c ON c.categoryID = j.categoryID 
LEFT JOIN supercategories s ON s.supercategoryID = c.supercategoryID 
LEFT JOIN users u ON u.userID = j.userID 
LEFT JOIN city ci ON ci.cityID = j.cityID 
WHERE j.jobStatus = 'completed' AND 
     DATE(j.jobDate) BETWEEN '2016-04-01' AND DATE(NOW()) 
GROUP BY u.userID 

查詢輸出:

======================================================== 
| userID | April 2016 | May 2016 | .... | January 2017 | 
======================================================== 
| 1234 | 1500 | 0  | .... |  0  | 
======================================================== 

所有的行正在轉換到第一個出現列。如果我添加GROUP BY MONTHNAME(jobDate)然後我得到每個用戶多行。你能幫助在這裏可能是錯誤的嗎?

+2

這類型的表樞軸需要動態的sql查詢檢查http://buysql.com/mysql/14-how-to-automate-pivot-tables.html – Blank

+2

當你必須滾動閱讀查詢時,現在是時候儘量減少問題! – jarlh

+0

這些是case表達式,不是語句 – jarlh

回答

1

你只是忘了對案件總和,看到前兩個月, 必須2016年4月期間投入總和,因爲你每月有多個記錄(你可以看到你有兩行

 SELECT u.userID, u.userFullName, ci.cityName, 
     GROUP_CONCAT(CASE WHEN CONCAT(MONTHNAME(j.jobDate), ' ', YEAR(j.jobDate)) = 'April 2016' THEN CONCAT(c.categoryName, ' ', s.supercategoryName) 
     END) AS 'April 2016 Services', 
     SUM(CASE WHEN CONCAT(MONTHNAME(j.jobDate), ' ', YEAR(j.jobDate)) = 'April 2016' THEN ROUND(j.totalCost + j.discountReceived) 
      ELSE 0 
     END) AS 'April 2016 Spends', 
    FROM jobs j 
    LEFT JOIN categories c ON c.categoryID = j.categoryID 
    LEFT JOIN supercategories s ON s.supercategoryID = c.supercategoryID 
    LEFT JOIN users u ON u.userID = j.userID 
    LEFT JOIN city ci ON ci.cityID = j.cityID 
    WHERE j.jobStatus = 'completed' AND 
      DATE(j.jobDate) BETWEEN '2016-04-01' AND DATE(NOW()) 
    GROUP BY u.userID 
+0

添加SUM()會給出錯誤 - 「組函數的使用無效」。對於2016年4月的服務,「CONCAT」函數會生成字符串,而2016年4月的支出在CASE語句中已經有一個「SUM」。 –

+0

找出問題,需要在'CASE'語句上應用'GROUP_CONCAT'函數。我似乎無法編輯您的答案,請進行更改以便我可以批准。 –

+0

我忘了刪除案件中的金額,我從group_concat中刪除了金額 – user3600910