我有一個表的工作具有以下結構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)
然後我得到每個用戶多行。你能幫助在這裏可能是錯誤的嗎?
這類型的表樞軸需要動態的sql查詢檢查http://buysql.com/mysql/14-how-to-automate-pivot-tables.html – Blank
當你必須滾動閱讀查詢時,現在是時候儘量減少問題! – jarlh
這些是case表達式,不是語句 – jarlh